Re: string vs. w/char*

2011-03-04 Thread Kagamin
Kagamin Wrote:

> Tyro[a.c.edwards] Wrote:
> 
> > class TopWinClass: WinClass
> > {
> > this(ushort resId, HINSTANCE hInst, WNDPROC wndProc)
> > {
> > super(resId, hInst, wndProc);
> > SetResIcons(resId);
> > wc.lpszMenuName = MAKEINTRESOURCEA(resId);  // [PROBLEM]
> > }
> > }
> > 
> > Every access to wc.lpszMenuName after this point fails.
> 
> Who accesses it?
> 
> MAKEINTRESOURCEA just casts int to char* so it's an invalid pointer while 
> still valid resource indentifier, windows can differentiate them by zeroed 
> high word.

casts ushort to char*


Re: string vs. w/char*

2011-03-04 Thread Kagamin
Tyro[a.c.edwards] Wrote:

> class TopWinClass: WinClass
> {
>   this(ushort resId, HINSTANCE hInst, WNDPROC wndProc)
>   {
>   super(resId, hInst, wndProc);
>   SetResIcons(resId);
>   wc.lpszMenuName = MAKEINTRESOURCEA(resId);  // [PROBLEM]
>   }
> }
> 
> Every access to wc.lpszMenuName after this point fails.

Who accesses it?

MAKEINTRESOURCEA just casts int to char* so it's an invalid pointer while still 
valid resource indentifier, windows can differentiate them by zeroed high word.


Re: string vs. w/char*

2011-03-03 Thread Tyro[a.c.edwards]

On 3/1/2011 8:25 PM, Tyro[a.c.edwards] wrote:

On 3/1/2011 7:18 PM, Bekenn wrote:

On 3/1/2011 12:25 AM, Tyro[a.c.edwards] wrote:

Nevertheless, execution haults
at the very next line following/catch and Create() never returns.


CreateWindow sends a few messages to your window proc; anything
interesting happening there?


Not sure how to check those messages, but I guess that's a cue for me to
do some more research. Off to Google land I go...


After much searching, I've finally located the actual cause of the problem:

class TopWinClass: WinClass
{
this(ushort resId, HINSTANCE hInst, WNDPROC wndProc)
{
super(resId, hInst, wndProc);
SetResIcons(resId);
wc.lpszMenuName = MAKEINTRESOURCEA(resId);  // [PROBLEM]
}
}

The root cause of the problem begins with a lack of understanding of how 
to create a proper resource file for D. I simply took the C++ version, 
compiled it with rcc and linked it to my project: No changes whatsoever. 
Turn out that made the compiler/linker stop complaining, however 
MAKEINTRESOURCEA(resId) still cannot locate the correct resources or 
cannot properly use the resource it finds to initialize wc.lpszMenuName. 
Every access to wc.lpszMenuName after this point fails. If initialize 
wc.lpszMenuName with one of the default strings, say "STATIC" at this 
point, the program runs to completion. I've attached the resource file, 
it hopes that someone could help me with it's conversion.


Thanks.
//Microsoft Developer Studio generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include 

/
#undef APSTUDIO_READONLY_SYMBOLS

/
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

/
//
// Dialog
//

IDD_ABOUT DIALOG DISCARDABLE  20, 20, 145, 82
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE
FONT 8, "MS Sans Serif"
BEGIN
DEFPUSHBUTTON   "OK",IDOK,47,62,50,14,WS_GROUP
ICONID_RS,IDC_RS,7,7,20,20,WS_GROUP
CTEXT   "Generic",IDC_STATIC,51,17,42,11
CTEXT   "(c) Reliable Software 1997, 98",IDC_STATIC,19,30,103,11
CTEXT   "http://www.relisoft.com",IDC_STATIC,7,47,130,10
END


#ifdef APSTUDIO_INVOKED
/
//
// TEXTINCLUDE
//

1 TEXTINCLUDE DISCARDABLE 
BEGIN
"resource.h\0"
END

2 TEXTINCLUDE DISCARDABLE 
BEGIN
"#include \r\n"
"\0"
END

3 TEXTINCLUDE DISCARDABLE 
BEGIN
"\r\n"
"\0"
END

#endif// APSTUDIO_INVOKED


/
//
// Icon
//

// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
ID_MAIN ICONDISCARDABLE "generic.ico"
ID_RS   ICONDISCARDABLE "rs.ico"

/
//
// Menu
//

ID_MAIN MENU DISCARDABLE 
BEGIN
POPUP "&Program"
BEGIN
MENUITEM "&About...",   IDM_ABOUT
MENUITEM SEPARATOR
MENUITEM "E&xit",   IDM_EXIT
END
POPUP "&Help", HELP
BEGIN
MENUITEM "&Please", IDM_HELP
END
END


/
//
// String Table
//

STRINGTABLE DISCARDABLE 
BEGIN
ID_MAIN "GenericClass"
ID_CAPTION  "Generic Windows Program"
END

#endif// English (U.S.) resources
/



#ifndef APSTUDIO_INVOKED
/
//
// Generated from the TEXTINCLUDE 3 resource.
//


/
#endif// not APSTUDIO_INVOKED



Re: string vs. w/char*

2011-03-01 Thread Tyro[a.c.edwards]

On 3/1/2011 7:18 PM, Bekenn wrote:

On 3/1/2011 12:25 AM, Tyro[a.c.edwards] wrote:

Nevertheless, execution haults
at the very next line following/catch and Create() never returns.


CreateWindow sends a few messages to your window proc; anything
interesting happening there?


Not sure how to check those messages, but I guess that's a cue for me to 
do some more research. Off to Google land I go...


Re: string vs. w/char*

2011-03-01 Thread Bekenn

On 3/1/2011 12:25 AM, Tyro[a.c.edwards] wrote:

 Nevertheless, execution haults
at the very next line following/catch and Create() never returns.


CreateWindow sends a few messages to your window proc; anything 
interesting happening there?


Re: string vs. w/char*

2011-03-01 Thread Tyro[a.c.edwards]
== Quote from Denis Koroskin (2kor...@gmail.com)'s article
> On Tue, 01 Mar 2011 02:08:48 +0300, Tyro[a.c.edwards]

> wrote:
> > == Quote from Denis Koroskin (2kor...@gmail.com)'s article
> >> On Mon, 28 Feb 2011 19:35:47 +0300, Tyro[a.c.edwards]
> > 
> >> wrote:
> >> > On 2/28/2011 11:08 PM, J Chapman wrote:
> >> >> == Quote from Tyro[a.c.edwards] (nos...@home.com)'s article
> >> >>> Both implementations results in error code 1812 being
> > returned from
> >> >>> GetLastError. explanation of the code reads:
> >> >>>ERROR_RESOURCE_DATA_NOT_FOUND
> >> >>>1812 (0x714)
> >> >>>The specified image file did not contain a resource
> > section.
> >> >>> The code I'm porting initially consisted of a resource.h
> > file, a
> >> >>> generic.rc file and two icons. I have not tried to include
> > the icons
> >> >>> and
> >> >>> generic.rc file in the compilation because I do not know
how
> > to as yet
> >> >>> and I've only used half of the resource.h file: didn't
think
> > I need the
> >> >>> whole thing. Could this be the reason for the error? If so
> > could you
> >> >>> direct me to the explanation of how to prepare these files
> > for
> >> >>> inclusion
> >> >>> in the compilation process?
> >> >>> Thanks,
> >> >>> Andrew
> >> >>
> >> >> You need to compile the .rc file (see
> >> >> http://www.digitalmars.com/ctg/rcc.html), then add the
> > resulting .res
> >> >> file
> >> >> to dmd's command line.
> >> >
> >> > Awesome, this does the trick. However I get get a "GP
Fault"?
> > during
> >> > execution. Using windbg, I tracked it down to this piece of
> > code:
> >> >
> >> > void Create()
> >> > {
> >> >_hwnd = CreateWindowExA(
> >> >  _exStyle,
> >> >  cast(const(char*))_wc.GetName(), // returns string
> >> >  cast(const(char*))_windowName,   // string variable
> >> >  _style,
> >> >  _x,
> >> >  _y,
> >> >  _width,
> >> >  _height,
> >> >  _hWndParent,
> >> >  _hMenu,
> >> >  _wc.GetInstance(),
> >> >  _data);
> >> >
> >> >  assert(_hwnd, "Internal error: Window Creation
Failed.");
> >> > }
> >> >
> >> > The program craps at assert() but the error is generated. It
> > just
> >> > displays a dialog box with the message: "test.exe has
stopped
> > working,
> >> > Windows is checking for a solution to the problem..."
> >> >
> >> > I'm thinking that _hwnd was never initialized and that
assert
> > is access
> >> > a null pointer but I cannot be sure. Any suggestions or
ideas?
> >> The
> >> >  cast(const(char*))_wc.GetName()
> >> line look *very* suspicious. You can't get a string and just
> > cast it to
> >> const(char)*. Most importantly, the string (most likely) is
not
> >> null-terminated.
> >> What you need to do here is the following:
> >> auto className = toStringz(_ws.GetName());
> >> auto caption = toStringz(_windowName);
> >> and pass those 2 to the function.
> >
> > Actually I've already tried that, it has no effect on the
outcome.
> > From your suggestion though, I've gone back and replace all the
> > cast(const(char*)) usage throughout the program. Final verdict:
> > the program still crashes it the same location. It actually
never
> > returns from CreateWindowExA().
> >
> >> Alternatively, you could make sure your strings are null-
> > terminated and
> >> pass the pointer directly (e.g. _windowName.ptr):
> >> string _windowName = "foo"; // null-terminated automatically
> >> string _caption = ("Hello, World" ~ "\0")[0..$-1]; // append
> > trailing zero
> >> to an existing string but exclude it from result (so that it's
> > not
> >> included in _caption.length)
> >
> This is indeed strange, but it has nothing to do with the
function itself.
> I still think the parameters you are passing might be invalid.
Try setting
> them to default values and see if that helps. Also try wrapping
the call
> with a try/catch block and output an exception you are getting
(if any).


The problem occurs at the site of the assertion. I wrapped the
function in a try/catch block and placed a call to MessageBoxA()
on either end of the the try block. Both calls to MessageBox fires
and the appropriate messages displayed. No exception is thrown:
made evident my the fact that the third call to MessageBox,
embeded in catch{}, is not fired. Nevertheless, execution haults
at the very next line following/catch and Create() never returns.


Re: string vs. w/char*

2011-02-28 Thread Denis Koroskin
On Tue, 01 Mar 2011 02:08:48 +0300, Tyro[a.c.edwards]   
wrote:



== Quote from Denis Koroskin (2kor...@gmail.com)'s article

On Mon, 28 Feb 2011 19:35:47 +0300, Tyro[a.c.edwards]



wrote:
> On 2/28/2011 11:08 PM, J Chapman wrote:
>> == Quote from Tyro[a.c.edwards] (nos...@home.com)'s article
>>> Both implementations results in error code 1812 being

returned from

>>> GetLastError. explanation of the code reads:
>>>ERROR_RESOURCE_DATA_NOT_FOUND
>>>1812 (0x714)
>>>The specified image file did not contain a resource

section.

>>> The code I'm porting initially consisted of a resource.h

file, a

>>> generic.rc file and two icons. I have not tried to include

the icons

>>> and
>>> generic.rc file in the compilation because I do not know how

to as yet

>>> and I've only used half of the resource.h file: didn't think

I need the

>>> whole thing. Could this be the reason for the error? If so

could you

>>> direct me to the explanation of how to prepare these files

for

>>> inclusion
>>> in the compilation process?
>>> Thanks,
>>> Andrew
>>
>> You need to compile the .rc file (see
>> http://www.digitalmars.com/ctg/rcc.html), then add the

resulting .res

>> file
>> to dmd's command line.
>
> Awesome, this does the trick. However I get get a "GP Fault"?

during

> execution. Using windbg, I tracked it down to this piece of

code:

>
> void Create()
> {
>_hwnd = CreateWindowExA(
>  _exStyle,
>  cast(const(char*))_wc.GetName(), // returns string
>  cast(const(char*))_windowName,   // string variable
>  _style,
>  _x,
>  _y,
>  _width,
>  _height,
>  _hWndParent,
>  _hMenu,
>  _wc.GetInstance(),
>  _data);
>
>  assert(_hwnd, "Internal error: Window Creation Failed.");
> }
>
> The program craps at assert() but the error is generated. It

just

> displays a dialog box with the message: "test.exe has stopped

working,

> Windows is checking for a solution to the problem..."
>
> I'm thinking that _hwnd was never initialized and that assert

is access

> a null pointer but I cannot be sure. Any suggestions or ideas?
The
>  cast(const(char*))_wc.GetName()
line look *very* suspicious. You can't get a string and just

cast it to

const(char)*. Most importantly, the string (most likely) is not
null-terminated.
What you need to do here is the following:
auto className = toStringz(_ws.GetName());
auto caption = toStringz(_windowName);
and pass those 2 to the function.


Actually I've already tried that, it has no effect on the outcome.
From your suggestion though, I've gone back and replace all the
cast(const(char*)) usage throughout the program. Final verdict:
the program still crashes it the same location. It actually never
returns from CreateWindowExA().


Alternatively, you could make sure your strings are null-

terminated and

pass the pointer directly (e.g. _windowName.ptr):
string _windowName = "foo"; // null-terminated automatically
string _caption = ("Hello, World" ~ "\0")[0..$-1]; // append

trailing zero

to an existing string but exclude it from result (so that it's

not

included in _caption.length)




This is indeed strange, but it has nothing to do with the function itself.  
I still think the parameters you are passing might be invalid. Try setting  
them to default values and see if that helps. Also try wrapping the call  
with a try/catch block and output an exception you are getting (if any).


Re: string vs. w/char*

2011-02-28 Thread Tyro[a.c.edwards]
== Quote from Denis Koroskin (2kor...@gmail.com)'s article
> On Mon, 28 Feb 2011 19:35:47 +0300, Tyro[a.c.edwards]

> wrote:
> > On 2/28/2011 11:08 PM, J Chapman wrote:
> >> == Quote from Tyro[a.c.edwards] (nos...@home.com)'s article
> >>> Both implementations results in error code 1812 being
returned from
> >>> GetLastError. explanation of the code reads:
> >>>ERROR_RESOURCE_DATA_NOT_FOUND
> >>>1812 (0x714)
> >>>The specified image file did not contain a resource
section.
> >>> The code I'm porting initially consisted of a resource.h
file, a
> >>> generic.rc file and two icons. I have not tried to include
the icons
> >>> and
> >>> generic.rc file in the compilation because I do not know how
to as yet
> >>> and I've only used half of the resource.h file: didn't think
I need the
> >>> whole thing. Could this be the reason for the error? If so
could you
> >>> direct me to the explanation of how to prepare these files
for
> >>> inclusion
> >>> in the compilation process?
> >>> Thanks,
> >>> Andrew
> >>
> >> You need to compile the .rc file (see
> >> http://www.digitalmars.com/ctg/rcc.html), then add the
resulting .res
> >> file
> >> to dmd's command line.
> >
> > Awesome, this does the trick. However I get get a "GP Fault"?
during
> > execution. Using windbg, I tracked it down to this piece of
code:
> >
> > void Create()
> > {
> >_hwnd = CreateWindowExA(
> >  _exStyle,
> >  cast(const(char*))_wc.GetName(), // returns string
> >  cast(const(char*))_windowName,   // string variable
> >  _style,
> >  _x,
> >  _y,
> >  _width,
> >  _height,
> >  _hWndParent,
> >  _hMenu,
> >  _wc.GetInstance(),
> >  _data);
> >
> >  assert(_hwnd, "Internal error: Window Creation Failed.");
> > }
> >
> > The program craps at assert() but the error is generated. It
just
> > displays a dialog box with the message: "test.exe has stopped
working,
> > Windows is checking for a solution to the problem..."
> >
> > I'm thinking that _hwnd was never initialized and that assert
is access
> > a null pointer but I cannot be sure. Any suggestions or ideas?
> The
> >  cast(const(char*))_wc.GetName()
> line look *very* suspicious. You can't get a string and just
cast it to
> const(char)*. Most importantly, the string (most likely) is not
> null-terminated.
> What you need to do here is the following:
> auto className = toStringz(_ws.GetName());
> auto caption = toStringz(_windowName);
> and pass those 2 to the function.

Actually I've already tried that, it has no effect on the outcome.
>From your suggestion though, I've gone back and replace all the
cast(const(char*)) usage throughout the program. Final verdict:
the program still crashes it the same location. It actually never
returns from CreateWindowExA().

> Alternatively, you could make sure your strings are null-
terminated and
> pass the pointer directly (e.g. _windowName.ptr):
> string _windowName = "foo"; // null-terminated automatically
> string _caption = ("Hello, World" ~ "\0")[0..$-1]; // append
trailing zero
> to an existing string but exclude it from result (so that it's
not
> included in _caption.length)



Re: string vs. w/char*

2011-02-28 Thread Denis Koroskin
On Mon, 28 Feb 2011 19:35:47 +0300, Tyro[a.c.edwards]   
wrote:



On 2/28/2011 11:08 PM, J Chapman wrote:

== Quote from Tyro[a.c.edwards] (nos...@home.com)'s article

Both implementations results in error code 1812 being returned from
GetLastError. explanation of the code reads:
   ERROR_RESOURCE_DATA_NOT_FOUND
   1812 (0x714)
   The specified image file did not contain a resource section.
The code I'm porting initially consisted of a resource.h file, a
generic.rc file and two icons. I have not tried to include the icons  
and

generic.rc file in the compilation because I do not know how to as yet
and I've only used half of the resource.h file: didn't think I need the
whole thing. Could this be the reason for the error? If so could you
direct me to the explanation of how to prepare these files for  
inclusion

in the compilation process?
Thanks,
Andrew


You need to compile the .rc file (see
http://www.digitalmars.com/ctg/rcc.html), then add the resulting .res  
file

to dmd's command line.


Awesome, this does the trick. However I get get a "GP Fault"? during  
execution. Using windbg, I tracked it down to this piece of code:


void Create()
{
   _hwnd = CreateWindowExA(
 _exStyle,
 cast(const(char*))_wc.GetName(), // returns string
 cast(const(char*))_windowName,   // string variable
 _style,
 _x,
 _y,
 _width,
 _height,
 _hWndParent,
 _hMenu,
 _wc.GetInstance(),
 _data);

 assert(_hwnd, "Internal error: Window Creation Failed.");
}

The program craps at assert() but the error is generated. It just  
displays a dialog box with the message: "test.exe has stopped working,  
Windows is checking for a solution to the problem..."


I'm thinking that _hwnd was never initialized and that assert is access  
a null pointer but I cannot be sure. Any suggestions or ideas?


The


 cast(const(char*))_wc.GetName()


line look *very* suspicious. You can't get a string and just cast it to  
const(char)*. Most importantly, the string (most likely) is not  
null-terminated.


What you need to do here is the following:

auto className = toStringz(_ws.GetName());
auto caption = toStringz(_windowName);

and pass those 2 to the function.

Alternatively, you could make sure your strings are null-terminated and  
pass the pointer directly (e.g. _windowName.ptr):


string _windowName = "foo"; // null-terminated automatically
string _caption = ("Hello, World" ~ "\0")[0..$-1]; // append trailing zero  
to an existing string but exclude it from result (so that it's not  
included in _caption.length)


Re: string vs. w/char*

2011-02-28 Thread Tyro[a.c.edwards]

On 2/28/2011 11:08 PM, J Chapman wrote:

== Quote from Tyro[a.c.edwards] (nos...@home.com)'s article

Both implementations results in error code 1812 being returned from
GetLastError. explanation of the code reads:
   ERROR_RESOURCE_DATA_NOT_FOUND
   1812 (0x714)
   The specified image file did not contain a resource section.
The code I'm porting initially consisted of a resource.h file, a
generic.rc file and two icons. I have not tried to include the icons and
generic.rc file in the compilation because I do not know how to as yet
and I've only used half of the resource.h file: didn't think I need the
whole thing. Could this be the reason for the error? If so could you
direct me to the explanation of how to prepare these files for inclusion
in the compilation process?
Thanks,
Andrew


You need to compile the .rc file (see
http://www.digitalmars.com/ctg/rcc.html), then add the resulting .res file
to dmd's command line.


Awesome, this does the trick. However I get get a "GP Fault"? during 
execution. Using windbg, I tracked it down to this piece of code:


void Create()
{
  _hwnd = CreateWindowExA(
_exStyle,
cast(const(char*))_wc.GetName(), // returns string
cast(const(char*))_windowName,   // string variable
_style,
_x,
_y,
_width,
_height,
_hWndParent,
_hMenu,
_wc.GetInstance(),
_data);

assert(_hwnd, "Internal error: Window Creation Failed.");
}

The program craps at assert() but the error is generated. It just 
displays a dialog box with the message: "test.exe has stopped working, 
Windows is checking for a solution to the problem..."


I'm thinking that _hwnd was never initialized and that assert is access 
a null pointer but I cannot be sure. Any suggestions or ideas?


Re: string vs. w/char*

2011-02-28 Thread Andrej Mitrovic
I've successfully used resource files with DFL. Maybe this will help:
http://www.dsource.org/forums/viewtopic.php?t=5591&sid=bf2d804f1d5a3f9efccbf29ebb6cf723

You'll have to dig into the DFL library sources to find out exactly
how it loads a resource file though.


Re: string vs. w/char*

2011-02-28 Thread J Chapman
== Quote from Tyro[a.c.edwards] (nos...@home.com)'s article
> Both implementations results in error code 1812 being returned from
> GetLastError. explanation of the code reads:
>   ERROR_RESOURCE_DATA_NOT_FOUND
>   1812 (0x714)
>   The specified image file did not contain a resource section.
> The code I'm porting initially consisted of a resource.h file, a
> generic.rc file and two icons. I have not tried to include the icons and
> generic.rc file in the compilation because I do not know how to as yet
> and I've only used half of the resource.h file: didn't think I need the
> whole thing. Could this be the reason for the error? If so could you
> direct me to the explanation of how to prepare these files for inclusion
> in the compilation process?
> Thanks,
> Andrew

You need to compile the .rc file (see
http://www.digitalmars.com/ctg/rcc.html), then add the resulting .res file
to dmd's command line.


Re: string vs. w/char*

2011-02-28 Thread Steven Schveighoffer
On Mon, 28 Feb 2011 08:30:02 -0500, Tyro[a.c.edwards]   
wrote:



On 2/28/2011 9:58 PM, Steven Schveighoffer wrote:

On Mon, 28 Feb 2011 07:34:39 -0500, Tyro[a.c.edwards] 
wrote:


The bellow code attempts to use LoadStringA() to initialize _buf.
However, regardless of what form _buf takes, the body of the if
statement is always executed. I've attempted to use every type of
string available in D to include char* _buf[MAX_RESSTRING+1] and
setting _buf[MAX_RESSTRING] = '\0'; What am I doing incorrectly?
Any assistance is greatly appreciated.

class ResString
{
enum { MAX_RESSTRING = 255 }

alias getBuffer this;
@property string getBuffer() { return _buf; }

this(HINSTANCE hInst, int resId)
{
_buf.length = MAX_RESSTRING;

SetLastError(0);

if(!LoadStringA(hInst, resId, cast(char*)toStringz(_buf), _buf.length
+ 1))
{
throw new WinException("Load String failed");
}
}

private:
string _buf;
}


You should not be overwriting buf, it is immutable. You need to make a
new buffer each time.

this(HINSTANCE hInst, int resId)
{

auto mybuf = new char[MAX_RESSTRING];
auto nchars = LoadStringA(hInst, resId, mybuf.ptr, mybuf.length);
if(!nchars)
{
throw new WinException("Load String failed");
}
_buf = assumeUnique(mybuf[0..nchars]);

SetLastError(0);
}

If this isn't working, you might consider that the string you are trying
to load doesn't actually exist (that is a valid condition). What is the
error from GetLastError ?

-Steve


Both implementations results in error code 1812 being returned from  
GetLastError. explanation of the code reads:


  ERROR_RESOURCE_DATA_NOT_FOUND
  1812 (0x714)
  The specified image file did not contain a resource section.

The code I'm porting initially consisted of a resource.h file, a  
generic.rc file and two icons. I have not tried to include the icons and  
generic.rc file in the compilation because I do not know how to as yet  
and I've only used half of the resource.h file: didn't think I need the  
whole thing. Could this be the reason for the error? If so could you  
direct me to the explanation of how to prepare these files for inclusion  
in the compilation process?




No clue, sorry.  I build D mostly on linux, on windows only when I have  
to.  Look on digitalmars.com for Windows programming.  Or try google.


-Steve


Re: string vs. w/char*

2011-02-28 Thread Tyro[a.c.edwards]

On 2/28/2011 9:58 PM, Steven Schveighoffer wrote:

On Mon, 28 Feb 2011 07:34:39 -0500, Tyro[a.c.edwards] 
wrote:


The bellow code attempts to use LoadStringA() to initialize _buf.
However, regardless of what form _buf takes, the body of the if
statement is always executed. I've attempted to use every type of
string available in D to include char* _buf[MAX_RESSTRING+1] and
setting _buf[MAX_RESSTRING] = '\0'; What am I doing incorrectly?
Any assistance is greatly appreciated.

class ResString
{
enum { MAX_RESSTRING = 255 }

alias getBuffer this;
@property string getBuffer() { return _buf; }

this(HINSTANCE hInst, int resId)
{
_buf.length = MAX_RESSTRING;

SetLastError(0);

if(!LoadStringA(hInst, resId, cast(char*)toStringz(_buf), _buf.length
+ 1))
{
throw new WinException("Load String failed");
}
}

private:
string _buf;
}


You should not be overwriting buf, it is immutable. You need to make a
new buffer each time.

this(HINSTANCE hInst, int resId)
{

auto mybuf = new char[MAX_RESSTRING];
auto nchars = LoadStringA(hInst, resId, mybuf.ptr, mybuf.length);
if(!nchars)
{
throw new WinException("Load String failed");
}
_buf = assumeUnique(mybuf[0..nchars]);

SetLastError(0);
}

If this isn't working, you might consider that the string you are trying
to load doesn't actually exist (that is a valid condition). What is the
error from GetLastError ?

-Steve


Both implementations results in error code 1812 being returned from 
GetLastError. explanation of the code reads:


 ERROR_RESOURCE_DATA_NOT_FOUND
 1812 (0x714)
 The specified image file did not contain a resource section.

The code I'm porting initially consisted of a resource.h file, a 
generic.rc file and two icons. I have not tried to include the icons and 
generic.rc file in the compilation because I do not know how to as yet 
and I've only used half of the resource.h file: didn't think I need the 
whole thing. Could this be the reason for the error? If so could you 
direct me to the explanation of how to prepare these files for inclusion 
in the compilation process?


Thanks,
Andrew


Re: string vs. w/char*

2011-02-28 Thread Steven Schveighoffer
On Mon, 28 Feb 2011 07:34:39 -0500, Tyro[a.c.edwards]   
wrote:


The bellow code attempts to use LoadStringA() to initialize _buf.  
However, regardless of what form _buf takes, the body of the if  
statement is always executed. I've attempted to use every type of string  
available in D to include char* _buf[MAX_RESSTRING+1] and setting  
_buf[MAX_RESSTRING] = '\0'; What am I doing incorrectly?

Any assistance is greatly appreciated.

class ResString
{
   enum { MAX_RESSTRING = 255 }

   alias getBuffer this;
   @property string getBuffer() { return _buf; }

   this(HINSTANCE hInst, int resId)
   {
 _buf.length = MAX_RESSTRING;

 SetLastError(0);

 if(!LoadStringA(hInst, resId, cast(char*)toStringz(_buf),  
_buf.length + 1))

 {
   throw new WinException("Load String failed");
 }
   }

private:
   string _buf;
}


You should not be overwriting buf, it is immutable.  You need to make a  
new buffer each time.


this(HINSTANCE hInst, int resId)
{

  auto mybuf = new char[MAX_RESSTRING];
  auto nchars = LoadStringA(hInst, resId, mybuf.ptr, mybuf.length);
  if(!nchars)
  {
throw new WinException("Load String failed");
  }
  _buf = assumeUnique(mybuf[0..nchars]);

  SetLastError(0);
}

If this isn't working, you might consider that the string you are trying  
to load doesn't actually exist (that is a valid condition).  What is the  
error from GetLastError ?


-Steve


string vs. w/char*

2011-02-28 Thread Tyro[a.c.edwards]
The bellow code attempts to use LoadStringA() to initialize _buf. 
However, regardless of what form _buf takes, the body of the if 
statement is always executed. I've attempted to use every type of string 
available in D to include char* _buf[MAX_RESSTRING+1] and setting 
_buf[MAX_RESSTRING] = '\0'; What am I doing incorrectly?

Any assistance is greatly appreciated.

class ResString
{
  enum { MAX_RESSTRING = 255 }

  alias getBuffer this;
  @property string getBuffer() { return _buf; }

  this(HINSTANCE hInst, int resId)
  {
_buf.length = MAX_RESSTRING;

SetLastError(0);

if(!LoadStringA(hInst, resId, cast(char*)toStringz(_buf), 
_buf.length + 1))

{
  throw new WinException("Load String failed");
}
  }

private:
  string _buf;
}