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
[FIXED] Unable to import Win32 function (spoiler: I'm dumb) #460542
07/03/16 15:39
07/03/16 15:39
Joined: Jan 2005
Posts: 330
USA
M
MatAllum Offline OP
Senior Member
MatAllum  Offline OP
Senior Member
M

Joined: Jan 2005
Posts: 330
USA
Trying to point my game towards a reasonable save directory by way of GetEnvironmentVariable here, without much luck. Per instructions on the manual (and guidance from Microsoft's documentation), I added this code near the top of my script:

Code:
#include <windows.h>
DWORD WINAPI GetEnvironmentVariable(char* lpName, char* lpBuffer, DWORD nSize);
#define PRAGMA_API GetEnvironmentVariable;kernel32!GetEnvironmentVariableA



However, while this combination does compile without giving me any syntax errors (or telling me it's not in the DLL), it apparently does not manage to import, as when I try to call it in main(), I get the error "Empty function called":

Code:
str_cpy(save_dir, "");
    GetEnvironmentVariable("AllUsersProfile", _chr(save), 128);
    str_cat(save, "\\[game name redacted]");
    CreateDirectory(_chr(profileTempStr), 0);



Am I missing something here? Or is there a better way to point my save_dir to an acceptable location?

Re: Can't import a Win32 API function [Re: MatAllum] #460543
07/03/16 15:45
07/03/16 15:45
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
Use LoadLibrary / GetModuleHandle and GetProcAddress.

Code:
GetEnvironmentVariable = GetProcAddress(GetModuleHandleA("kernel32.dll"), "GetEnvironmentVariableA");


Last edited by Ch40zzC0d3r; 07/03/16 15:46.
Re: Can't import a Win32 API function [Re: Ch40zzC0d3r] #460551
07/04/16 01:17
07/04/16 01:17
Joined: Jan 2005
Posts: 330
USA
M
MatAllum Offline OP
Senior Member
MatAllum  Offline OP
Senior Member
M

Joined: Jan 2005
Posts: 330
USA
Looks like the perfect solution to me, but unfortunately I end up with the same problem still happening.

Re: Can't import a Win32 API function [Re: MatAllum] #460575
07/04/16 22:54
07/04/16 22:54
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
hm put my code into sevreal lines and add debug msgboxes to see which api call makes it crash.
In worst case I can give you a replacement function for GetModuleHandle and GetProcAddress (which is actually a little hacky but works as expected on all OS versions)

Last edited by Ch40zzC0d3r; 07/04/16 22:55.
Re: Can't import a Win32 API function [Re: Ch40zzC0d3r] #460656
07/08/16 01:49
07/08/16 01:49
Joined: Jan 2005
Posts: 330
USA
M
MatAllum Offline OP
Senior Member
MatAllum  Offline OP
Senior Member
M

Joined: Jan 2005
Posts: 330
USA
GetEnvironmentVariable is the one - tested by commenting. The setup functions that import it from the system DLL work fine without crashing the game, although whether they successfully import it remains to be seen.

Edit: You would think from the programming forums I've visited in the past that I'd have learned to post full working examples. Here is a short program to demonstrate the problem - assuming my computer isn't just borked, this example will produce the error "Empty function called" instead of properly creating the Test Game folder under ProgramData.

Code:
#include <acknex.h>
#include <windows.h>
DWORD WINAPI GetEnvironmentVariable(char* lpName, char* lpBuffer, DWORD nSize);

STRING* profileTempStr = "#128";
int main()
{
	GetEnvironmentVariable = GetProcAddress(GetModuleHandle("kernel32.dll"), "GetEnvironmentVariableA");
	str_cpy(profileTempStr, "");
	GetEnvironmentVariable("AllUsersProfile", _chr(profileTempStr), 128);
	str_cat(profileTempStr, "\\Test Game");
	CreateDirectory(_chr(profileTempStr), 0);
}


Last edited by MatAllum; 07/08/16 01:58. Reason: Added example
Re: Can't import a Win32 API function [Re: MatAllum] #460658
07/08/16 08:42
07/08/16 08:42
Joined: Mar 2012
Posts: 927
cyberspace
W
Wjbender Offline
User
Wjbender  Offline
User
W

Joined: Mar 2012
Posts: 927
cyberspace

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <windows.h>
///////////////////////////////

STRING* prof="#128";

void main()
{
	long result=GetEnvironmentVariable("AllUsersProfile",_chr(prof),128);
	str_cat(prof,"\\test");
	printf("%s",_chr(prof));
}



Compulsive compiler
Re: Can't import a Win32 API function [Re: MatAllum] #460659
07/08/16 08:47
07/08/16 08:47
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
With your code I get a crash because you included windows.h and overwrite an already declared function pointer.

Either rename your GetEnvironmentVariable to something else or do it like that:

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

STRING* profileTempStr = "#128";

function main()
{
	GetEnvironmentVariable("AllUsersProfile", _chr(profileTempStr), 128);
	printf(_chr(profileTempStr));
}


Last edited by Ch40zzC0d3r; 07/08/16 08:47.
Re: Can't import a Win32 API function [Re: Ch40zzC0d3r] #460660
07/08/16 09:00
07/08/16 09:00
Joined: Mar 2012
Posts: 927
cyberspace
W
Wjbender Offline
User
Wjbender  Offline
User
W

Joined: Mar 2012
Posts: 927
cyberspace
take a peek in your windows.h before importing already defined functions laugh as you can see 2 replies before this one , no need to redo what windows.h already did, remember to look at the returned result when you use that function too .


Compulsive compiler
Re: Can't import a Win32 API function [Re: Wjbender] #460666
07/08/16 23:24
07/08/16 23:24
Joined: Jan 2005
Posts: 330
USA
M
MatAllum Offline OP
Senior Member
MatAllum  Offline OP
Senior Member
M

Joined: Jan 2005
Posts: 330
USA
Strange. I could have sworn this did not work when I originally tried it. Either way, changing the name fixes it, as does using the original, at least here on this computer.


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