🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #15771

Re: RegQueryValueEx problem

#15771
From: robertbienert gmx.net robertbien...
Date: Wed, 18 Jun 2003 21:30
160 lines
6583 bytes
Remy Lebeau (TeamB) wrote:
> You still didn't answer my other questions, though - what are the return
> values of RegQueryValueEx() and GetLastError() when the code is failing?

I'm very sorry, but I haven't checked GetLastError so far, maybe because 
it isn't mentioned in my version of Win32.HLP from www.activevb.de. 
RegQueryValueEx gave back a value != ERROR_SUCCESS, but I didn't checked 
it, too.
I'll try your example and mail my experience with it, when I'm home from 
a short holiday on friday.

>>If I call RegCloseKey when RegQueryEx failed, I got an acces violation.
> 
> Then I would suspect that you are corrupting your memory in general.  Your
> buffer is probably invalid, and you are probably overflowing it and writing
> into surrounding memory, which is probably writting over your HKEY handle as
> well.  Please post more complete code snippets.

Here you are: That are the main c file plus the header file for both the 
dll and the using exe:

/* win32shell.h */
/* general error codes
  */
#define NO_MEM		-10
/* error codes for getShellFolder()
  */
#define WRONG_KEY	-20

/* Global definitions of the shell folders for using in getShellFolder().
  * The annotations are valid if you installed Windows in the default
  * folders.
  */
#define AdministrativeTools	 1
#define AppData			 2	// %WINDIR%\AppData
#define Cache			 3	// %WINDIR%\Temporary Internet Files
#define Cookies			 4	// %WINDIR%\Cookies
#define Desktop			 5	// %WINDIR%\Desktop
#define Favorites		 6	// %WINDIR%\Favorites
#define Fonts			 7	// %WINDIR%\Fonts
#define History			 8	// %WINDIR%\History
#define LocalAppData		 9	// %WINDIR%\Local Settings\AppData
#define MyPictures		10	// My Documents\My Pictures
#define NetHood			11	// %WINDIR%\NetHood
#define Personal		12	// My Documents
#define PrintHood		13	// %WINDIR%\PrintHood
#define Programs		14	// %WINDIR%\Start Menu\Programs
#define Recent			15	// %WINDIR%\Recent
#define SendTo			16	// %WINDIR%\SendTo
#define StartMenu		17	// %WINDIR%\Start Menu
#define Startup			18	// %WINDIR%\Start Menu\Programs\Startup
#define Templates		19	// %WINDIR%\ShellNew

/* Here starts the section with the exported/imported function prototypes
  */
#ifdef __cplusplus
extern "C" {
#endif

#ifdef BUILD_DLL
#define DLL_CALL __declspec(dllexport)
#else
#define DLL_CALL __declspec(dllimport)
#endif

	DLL_CALL int __stdcall getShellFolder(int folder, char *buffer);

#ifdef __cplusplus
}
#endif

/* win32shell.c */
#include <windows.h>
#include <stdlib.h>

#include <stdio.h>	//D

#define BUILD_DLL
#include "win32shell.h"

const char *ShellFoldersKey = "Software\\Microsoft\\Windows\\"
		"CurrentVersion\\Explorer\\Shell Folders";

int __stdcall getShellFolder(int folder, char *buffer) {
	HKEY regh = NULL;
	LPDWORD buffsize = 0;
	int retval = 0;
	char *regkey;// = NULL;

	//if ((regkey = (char *) malloc(strlen(ShellFoldersKey) + 51)) == NULL)
	//	return NO_MEM;

	//strcpy(regkey, ShellFoldersKey);
	switch(folder) {
		case AdministrativeTools:
			//strcat(regkey, "Administrative Tools"); break;
			regkey = "Administrative Tools"; break;
		case AppData:	//strcat(regkey, "AppData"); break;
			regkey = "AppData"; break;
		case Cache:	//strcat(regkey, "Cache"); break;
			regkey = "Cache"; break;
		case Cookies:	//strcat(regkey, "Cookies"); break;
			regkey = "Cookies"; break;
		case Desktop:	//strcat(regkey, "Desktop"); break;
			regkey = "Desktop"; break;
		case Favorites:	//strcat(regkey, "Favorites"); break;
			regkey = "Favorites"; break;
		case Fonts:	//strcat(regkey, "Fonts"); break;
			regkey = "Fonts"; break;
		case History:	//strcat(regkey, "History"); break;
			regkey = "History"; break;
		case LocalAppData:
			//strcat(regkey, "Local AppData"); break;
			regkey = "Local AppData"; break;
		case MyPictures:
			//strcat(regkey, "My Pictures"); break;
			regkey = "My Pictures"; break;
		case NetHood:	//strcat(regkey, "NetHood"); break;
			regkey = "NetHood"; break;
		case Personal:	//strcat(regkey, "Personal"); break;
			regkey = "Personal"; break;
		case PrintHood:	//strcat(regkey, "PrintHood"); break;
			regkey = "PrintHood"; break;
		case Programs:	//strcat(regkey, "Programs"); break;
			regkey = "Programs"; break;
		case Recent:	//strcat(regkey, "Recent"); break;
			regkey = "Recent"; break;
		case SendTo:	//strcat(regkey, "SendTo"); break;
			regkey = "SendTo"; break;
		case StartMenu:	//strcat(regkey, "Start Menu"); break;
			regkey = "Start Menu"; break;
		case Startup:	//strcat(regkey, "Startup"); break;
			regkey = "Startup"; break;
		case Templates:	//strcat(regkey, "Templates"); break;
			regkey = "Templates"; break;
		default: return WRONG_KEY;
	}

	if (RegOpenKeyEx(HKEY_CURRENT_USER, ShellFoldersKey, 0,
				KEY_READ, &regh) == ERROR_SUCCESS)
	{
		//char msg[1024];

		//if (RegQueryValueEx(regh, regkey, NULL, NULL, NULL, buffsize)
		//		== ERROR_SUCCESS) {
		//	buffer = (char *) malloc((unsigned int) buffsize + 1);
		MessageBox(0, regkey, "Debug", MB_OK);
		if(	RegQueryValueEx(regh, regkey, NULL, NULL, buffer,
					buffsize + 1) == ERROR_SUCCESS) {
			RegCloseKey(regh);
			retval = 1;
		}
	}
	return retval;
}

Yours,
-- 
Robert Bienert <robertbienert@gmx.net>
Check also my free software at http://www.rbprogs.de.vu/
Technical Details
Message-ID: <3ef0da0c@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-08!supernews.com!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <3eef5413@newsgroups.borland.com> <3eef5db8$1@newsgroups.borland.com> <3ef0879a$1@newsgroups.borland.com> <3ef09c68@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,5800567a1464c960
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-06-18 14:36:16 PST
Date: Wed, 18 Jun 2003 23:30:21 +0200
From: "robertbienert<AT>gmx.net" <"robertbienert<AT>gmx.net">
User-Agent: Mozilla/5.0 (Windows; U; Win98; de-AT; rv:1.2) Gecko/20021126
X-Accept-Language: de, en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: RegQueryValueEx problem
References: <3eef5413@newsgroups.borland.com> <3eef5db8$1@newsgroups.borland.com> <3ef0879a$1@newsgroups.borland.com> <3ef09c68@newsgroups.borland.com>
In-Reply-To: <3ef09c68@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 195.127.134.231
Message-ID: <3ef0da0c@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1055971852 195.127.134.231 (18 Jun 2003 14:30:52 -0700)
Lines: 161
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-08!supernews.com!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail