resources - delphi write data with updateresources -
i try write string external exe file, when read string via other project other string. example: when write "test123" read "test123click". when write "test" read "test.". when write "name" read "name.".
i use script write:
procedure writesettings(serverfile: string; settings: string); var resourcehandle: thandle; pwserverfile: pwidechar; begin getmem(pwserverfile, (length(serverfile) + 1) * 2); try stringtowidechar(serverfile, pwserverfile, length(serverfile) * 2); resourcehandle := beginupdateresourcew(pwserverfile, false); updateresourcew(resourcehandle, makeintresourcew(10), 'settings', 0, @settings[1], bytelength(settings)); endupdateresourcew(resourcehandle, false); freemem(pwserverfile); end; end;
this script read:
function loadsettings: string; var resourcelocation: hrsrc; resourcesize: dword; resourcehandle: thandle; resourcepointer: pointer; begin resourcelocation := findresource(hinstance, 'settings', rt_rcdata); resourcesize := sizeofresource(hinstance, resourcelocation); resourcehandle := loadresource(hinstance, resourcelocation); resourcepointer := lockresource(resourcehandle); if resourcepointer <> nil begin setlength(result, resourcesize - 1); copymemory(@result[1], resourcepointer, resourcesize); freeresource(resourcehandle); end; end;
what doing wrong? use everywhere normal string, not ansi , utf8.
you need write null terminator. every time call updateresourcew
pass (length(str)+1)*sizeof(str[1])
byte count parameter.
also, @settings[1]
fail empty string. i'd use pwidechar()
instead. so:
updateresourcew(resourcehandle, makeintresourcew(10), 'settings', 0, pwidechar(settings), (length(settings)+1)*sizeof(settings[1]));
although explicit w
suffixing rather needless. can write:
updateresource(resourcehandle, makeintresource(10), 'settings', 0, pchar(settings), (length(settings)+1)*sizeof(settings[1]));
and dynamic memory allocation needless. can write:
procedure writesettings(serverfile: string; settings: string); var resourcehandle: thandle; begin resourcehandle := beginupdateresource(pchar(serverfile), false); updateresource(resourcehandle, makeintresource(10), 'settings', 0, pchar(settings), (length(settings)+1)*sizeof(settings[1])); endupdateresource(resourcehandle, false); end;
and should include error checking:
procedure writesettings(serverfile: string; settings: string); var resourcehandle: thandle; begin resourcehandle := beginupdateresource(pchar(serverfile), false); win32check(resourcehandle<>0); win32check(updateresource(resourcehandle, makeintresource(10), 'settings', 0, pchar(settings), (length(settings)+1)*sizeof(settings[1]))); win32check(endupdateresource(resourcehandle, false)); end;
Comments
Post a Comment