Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
basik85278
by basik85278. 04/28/24 08:56
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Quad), 755 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Invalid memory area STR #401311
05/16/12 15:14
05/16/12 15:14
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Hi,

I have the following code to get My Documents folder:

Code:
#include <acknex.h>;
#include <windows.h>;

long __stdcall RegOpenKey(long hKey, char* lpSubKey, long phkResult);
#define PRAGMA_API RegOpenKey;advapi32!RegOpenKeyA
long __stdcall RegQueryValueEx(long hKey, char* lpValueName, long lpReserved, long lpType, long  lpData, long lpcbData);
#define PRAGMA_API RegQueryValueEx;advapi32!RegQueryValueExA
long __stdcall RegCloseKey(long hKey);
#define PRAGMA_API RegCloseKey;advapi32!RegCloseKey

var reg_get_STRING(long HKEY, char* KEY, char* VALUE, STRING* *DATA)
{	long regPtr=0, DataType=0, DataLen=999;
	if(RegOpenKey(HKEY, KEY, &regPtr))	return(false);
	if((RegQueryValueEx(regPtr,VALUE,0,&DataType,NULL,&DataLen))||(DataType!=1))
	{	RegCloseKey(regPtr);	return(false);		}
	STRING* tmpStr = str_create("");
	str_cat_num(tmpStr, "\#%.0f", DataLen);
	if(!*DATA)	*DATA = str_create("");
	str_cpy(*DATA, _chr(tmpStr));		str_remove(tmpStr);

	//!!!!=>>following line causes error "invalid memory area STR"<<=!!!!
	RegQueryValueEx(regPtr,VALUE,0,0,_chr(*DATA),&DataLen); 
	
	RegCloseKey(regPtr);	return(true);
}

void getMyDocuments()
{
	STRING* tmpStr = str_create("");
	
	reg_get_STRING(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Personal", tmpStr);
	str_cat(tmpStr, "\\My Game");
	str_cpy(save_dir, tmpStr);
	CreateDirectory(_chr(tmpStr),NULL);
	
	ptr_remove(tmpStr);

	error(save_dir);
}

void main()
{
	warn_level=6;
	getMyDocuments();
}



The code works (almost) well, it gives me the My Documents folder, but I get the error message "invalid memory area STR" in this line:
Code:
RegQueryValueEx(regPtr,VALUE,0,0,_chr(*DATA),&DataLen);



But I don't have any idea what's wrong.

Does anyone have an idea?

Re: Invalid memory area STR [Re: oliver2s] #401313
05/16/12 15:56
05/16/12 15:56
Joined: Jul 2008
Posts: 894
T
TechMuc Offline
User
TechMuc  Offline
User
T

Joined: Jul 2008
Posts: 894
Hi Oliver,
[edit: this post is full of advertisement, so just copy your code if you don't want to read laugh ]
Okay that's really senseless, but i just have to promote my product here.. With my wonderful perfect editor (liteEdit, for your reference: check this: http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401310#Post401310 ) i was able to REALLY debug your code and check all temporary values.

- enough.

The debugging result was, that the crash didn't happened in your mentioned line but in the str_cat(tmpStr, "\\my Game"); line. Why? Because the string is corrupted (can also be detected by the debugger tongue ), after the RegQueryValueEx instruction.
The string is currupted because you can not give a char* of a string to a WinAPI function. This might (!!) work in very few cases (when the string aquired by str_create is big enough), but most probable will dump, because the other internal struct members of STIRNG (e.g. "length") will be ignored / not mainted.

Use the following code to solve your problem, and use my editor to prevent those problems in future laugh

Code:
#include <acknex.h>;
#include <windows.h>;

long __stdcall RegOpenKey(long hKey, char* lpSubKey, long phkResult);
#define PRAGMA_API RegOpenKey;advapi32!RegOpenKeyA
long __stdcall RegQueryValueEx(long hKey, char* lpValueName, long lpReserved, long lpType, long  lpData, long lpcbData);
#define PRAGMA_API RegQueryValueEx;advapi32!RegQueryValueExA
long __stdcall RegCloseKey(long hKey);
#define PRAGMA_API RegCloseKey;advapi32!RegCloseKey

var reg_get_STRING(long HKEY, char* KEY, char* VALUE, STRING* *DATA)
{	long regPtr=0, DataType=0, DataLen=999;
	char* dwData = 0;
	dwData = (char*)malloc(sizeof(char*)*DataLen);
	memset(dwData,0,DataLen);
	if(RegOpenKey(HKEY, KEY, &regPtr))	return(false);
	if((RegQueryValueEx(regPtr,VALUE,0,&DataType,NULL,&DataLen))||(DataType!=1))
	{	RegCloseKey(regPtr);	return(false);		}
	STRING* tmpStr = str_create("");
	str_cat_num(tmpStr, "\#%.0f", DataLen);
	if(!*DATA)	*DATA = str_create("");
	str_cpy(*DATA, _chr(tmpStr));		str_remove(tmpStr);

	//!!!!=>>following line causes error "invalid memory area STR"<<=!!!!
	RegQueryValueEx(regPtr,VALUE,0,0,dwData,&DataLen); 
	str_cpy(*DATA,dwData);
	free(dwData);
	RegCloseKey(regPtr);	return(true);
}

void getMyDocuments()
{
	STRING* tmpStr = str_create("");
	
	reg_get_STRING(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Personal", tmpStr);
	str_cat(tmpStr, "\\My Game");
	str_cpy(save_dir, tmpStr);
	CreateDirectory(_chr(tmpStr),NULL);
	
	ptr_remove(tmpStr);

	error(save_dir);
}

void main()
{
	warn_level=6;
	getMyDocuments();
}



Last edited by TechMuc; 05/16/12 15:59.
Re: Invalid memory area STR [Re: TechMuc] #401315
05/16/12 16:04
05/16/12 16:04
Joined: Aug 2002
Posts: 3,258
Mainz
oliver2s Offline OP
Expert
oliver2s  Offline OP
Expert

Joined: Aug 2002
Posts: 3,258
Mainz
Wow, thank you for the fast answer. Seems it works laugh

And I will try your editor wink


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1