win32utils-devel@rubyforge.org
2004-Feb-25 11:53 UTC
[Win32utils-devel] Wide strings and LPCTSTR types
All, I was experimenting with converting LPCTSTR strings to wide strings with something like this: // Converts a Ruby string to a LPWSTR LPCTSTR AllocWideLString(VALUE rbString){ char* str = STR2CSTR(rbString); int length = (strlen(str)+1) * sizeof(WCHAR); LPCTSTR lpStr = (LPCTSTR)malloc(length); MultiByteToWideChar( CP_ACP, 0, str, strlen(str)+1, (LPWSTR)lpStr, length ); return lpStr; } But, that didn''t seem to work very well in testing. Is there a better way? Otherwise, I''ll have to stick to casting for now. Also, I noticed that there''s a IsTextUnicode() function. Should I be employing that somehow? Dan
win32utils-devel@rubyforge.org
2004-Feb-25 19:15 UTC
[Win32utils-devel] Wide strings and LPCTSTR types
Hi,> > All, > > I was experimenting with converting LPCTSTR strings to wide strings with > something like this: > > // Converts a Ruby string to a LPWSTR > LPCTSTR AllocWideLString(VALUE rbString){ > char* str = STR2CSTR(rbString); > int length = (strlen(str)+1) * sizeof(WCHAR); > LPCTSTR lpStr = (LPCTSTR)malloc(length); > > MultiByteToWideChar( > CP_ACP, > 0, > str, > strlen(str)+1, > (LPWSTR)lpStr, > length > ); > > return lpStr; > } > > But, that didn''t seem to work very well in testing. Is there a better > way? Otherwise, I''ll have to stick to casting for now. > > Also, I noticed that there''s a IsTextUnicode() function. Should I be > employing that somehow?Do You still have trouble with MultiByteToWideChar? Maybe MultiByteToWideChar hates you :) Would you try mbstowcs like following? nLen = strlen(szText) + 1; wszBuffer = malloc(nLen) * sizeof(WCHAR); mbstowcs(wszBuffer, szText, nLen); Regards, Park Heesob --MIME Multi-part separator--
win32utils-devel@rubyforge.org
2004-Feb-26 18:09 UTC
[Win32utils-devel] Wide strings and LPCTSTR types
> -----Original Message----- > From: win32utils-devel-bounces@rubyforge.org > [mailto:win32utils-devel-bounces@rubyforge.org] On Behalf Of > win32utils-devel@rubyforge.org > Sent: Wednesday, February 25, 2004 5:07 PM > To: win32utils-devel@rubyforge.org > Subject: Re:[Win32utils-devel] Wide strings and LPCTSTR types<snip>> Would you try mbstowcs like following? > > nLen = strlen(szText) + 1; > wszBuffer = malloc(nLen) * sizeof(WCHAR); > mbstowcs(wszBuffer, szText, nLen);I tried this: LPCTSTR AllocWideLPCString(VALUE rbString){ char* szText = STR2CSTR(rbString); int nLen = (strlen(szText) + 1) * sizeof(LPCTSTR); LPCTSTR wszBuffer = (LPCTSTR)malloc(nLen); mbstowcs(wszBuffer, szText, nLen); return wszBuffer; } It still didn''t work. Dan
win32utils-devel@rubyforge.org
2004-Feb-26 19:02 UTC
[Win32utils-devel] Wide strings and LPCTSTR types
> > > -----Original Message----- > > From: win32utils-devel-bounces@rubyforge.org > > [mailto:win32utils-devel-bounces@rubyforge.org] On Behalf Of > > win32utils-devel@rubyforge.org > > Sent: Wednesday, February 25, 2004 5:07 PM > > To: win32utils-devel@rubyforge.org > > Subject: Re:[Win32utils-devel] Wide strings and LPCTSTR types > <snip> > > Would you try mbstowcs like following? > > > > nLen = strlen(szText) + 1; > > wszBuffer = malloc(nLen) * sizeof(WCHAR); > > mbstowcs(wszBuffer, szText, nLen); > > I tried this: > > LPCTSTR AllocWideLPCString(VALUE rbString){ > char* szText = STR2CSTR(rbString); > int nLen = (strlen(szText) + 1) * sizeof(LPCTSTR);should be int nLen = (strlen(szText) + 1) * sizeof(WCHAR);> LPCTSTR wszBuffer = (LPCTSTR)malloc(nLen); > mbstowcs(wszBuffer, szText, nLen); > return wszBuffer; > } > > It still didn''t work. > > DanRegards, Park Heesob --MIME Multi-part separator--
win32utils-devel@rubyforge.org
2004-Feb-27 12:05 UTC
[Win32utils-devel] Wide strings and LPCTSTR types
> > I tried this: > > > > LPCTSTR AllocWideLPCString(VALUE rbString){ > > char* szText = STR2CSTR(rbString); > > int nLen = (strlen(szText) + 1) * sizeof(LPCTSTR); > should be > int nLen = (strlen(szText) + 1) * sizeof(WCHAR);I tried both actually. Didn''t make any difference. For testing, I''m using the Eventlog.backup method. in eventlog_backup: // original - this works lpBackupFileName = (LPCTSTR)STR2CSTR(rbFile); // proposed - this doesn''t work lpBackupFileName = AllocWideLPCString(rbFile); I tried this simple test script: # test_backup.rb require "ftools" require "win32/eventlog" include Win32 backup_file = "C:\\event_backup2" File.delete(backup_file) if File.exists?(backup_file) e2 = EventLog.open("Application") e2.backup(backup_file) e2.close test_backup.rb:20:in `backup'': BackupEventLog() call failed: Cannot create a file when that file already exists. (Win32::EventLogError) from test_backup.rb:20 If I use the old code, it works fine. What gives? Dan
win32utils-devel@rubyforge.org
2004-Feb-27 19:11 UTC
[Win32utils-devel] Wide strings and LPCTSTR types
> > > I tried this: > > > > > > LPCTSTR AllocWideLPCString(VALUE rbString){ > > > char* szText = STR2CSTR(rbString); > > > int nLen = (strlen(szText) + 1) * sizeof(LPCTSTR); > > should be > > int nLen = (strlen(szText) + 1) * sizeof(WCHAR); > > I tried both actually. Didn''t make any difference. For testing, I''m > using the Eventlog.backup method. > > in eventlog_backup: > > // original - this works > lpBackupFileName = (LPCTSTR)STR2CSTR(rbFile); > > // proposed - this doesn''t work > lpBackupFileName = AllocWideLPCString(rbFile); > > I tried this simple test script: > > # test_backup.rb > require "ftools" > require "win32/eventlog" > include Win32 > > backup_file = "C:\\event_backup2" > File.delete(backup_file) if File.exists?(backup_file) > > e2 = EventLog.open("Application") > e2.backup(backup_file) > e2.close > > test_backup.rb:20:in `backup'': BackupEventLog() call failed: Cannot > create a file when that file already exists. (Win32::EventLogError) > from test_backup.rb:20 > > If I use the old code, it works fine. What gives? >I mean you should use AllocWideLPCString when api require unicode string. You don''t need to use AllocWideLPCString if it works without it. :) Regards, Park Heesob