string vs. w/char*

Tyro[a.c.edwards] nospam at home.com
Mon Feb 28 05:30:02 PST 2011


On 2/28/2011 9:58 PM, Steven Schveighoffer wrote:
> On Mon, 28 Feb 2011 07:34:39 -0500, Tyro[a.c.edwards] <nospam at home.com>
> 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


More information about the Digitalmars-d-learn mailing list