From: "Berger, Daniel" Date: 2006-05-15T23:36:36+09:00 Subject: Re: Oddity with Win32API, RegQueryValueEx > -----Original Message----- > From: Park Heesob [mailto:phasis68@hotmail.com] > Sent: Monday, May 15, 2006 4:31 AM > To: ruby-talk ML > Subject: Re: Oddity with Win32API, RegQueryValueEx > > > > Hi, > > >From: Daniel Berger > >Reply-To: ruby-talk@ruby-lang.org > >To: ruby-talk@ruby-lang.org (ruby-talk ML) > >Subject: Oddity with Win32API, RegQueryValueEx > >Date: Mon, 15 May 2006 18:45:19 +0900 > > > >Hi all, > > > >Ruby 1.8.4 > >Windows XP > > > >I noticed something curious when using the RegQueryValueEx() > function > >using > >straight C versus a Win32API wrapper. Whenever I call it > via Win32API, it > >always returns ERROR_MORE_DATA (i.e. value 234) on the first > call, not > >matter what I set the buffer size to. > > > >Here's a sample C program that works fine: > > > >#include > >#include > > > >int main(){ > > char* key = > >"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\ > SNDSrvc"; > > char* val = "EventMessageFile"; > > char buf[1024]; > > DWORD size; > > int rv; > > HKEY hk; > > > > rv = RegOpenKeyEx( > > HKEY_LOCAL_MACHINE, > > key, > > 0, > > KEY_READ, > > &hk > > ); > > > > if(rv == ERROR_SUCCESS){ > > rv = RegQueryValueEx(hk, val, NULL, NULL, buf, &size); > > if(rv == ERROR_SUCCESS){ > > printf("Success!\n"); > > printf("Size: %i\n", size); > > printf("Buf: %s\n", buf); > > RegCloseKey(hk); > > } > > else{ > > printf("Failure: %i\n", GetLastError()); > > } > > } > > > > return 0; > >} > > > >However, the equivalent Ruby program always returns ERROR_MORE_DATA: > > > >require 'Win32API' > > > >HKEY_LOCAL_MACHINE = 0x80000002 > >KEY_READ = 0x20019 > > > >RegCloseKey = Win32API.new('advapi32', 'RegCloseKey', 'L', 'L') > >RegOpenKeyEx = Win32API.new('advapi32', 'RegOpenKeyEx', > 'LPLLP', 'L') > >RegQueryValueEx = Win32API.new('advapi32', > 'RegQueryValueEx', 'LPLPPP', > >'L') > > > >val = 'EventMessageFile' > >key = > >"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\SNDSrvc" > >hk = [0].pack('L') > > > >if RegOpenKeyEx.call(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, hk) == 0 > > hk = hk.unpack('L').first > > buf = 0.chr * 1024 > > size = 0.chr * 4 > > should be > size = [buf.length].pack('L') Ah, ok. I thought it would just set the size regardless. Thanks, Dan This communication is the property of Qwest and may contain confidential or privileged information. Unauthorized use of this communication is strictly prohibited and may be unlawful. If you have received this communication in error, please immediately notify the sender by reply e-mail and destroy all copies of the communication and any attachments.