0 registered members (),
1,094
guests, and 1
spider. |
Key:
Admin,
Global Mod,
Mod
|
|
|
[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
MatAllum
OP
Senior Member
|
OP
Senior Member
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:
#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":
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
Ch40zzC0d3r
Serious User
|
Serious User
Joined: Oct 2011
Posts: 1,082
Germany
|
Use LoadLibrary / GetModuleHandle and GetProcAddress.
GetEnvironmentVariable = GetProcAddress(GetModuleHandleA("kernel32.dll"), "GetEnvironmentVariableA");
Last edited by Ch40zzC0d3r; 07/03/16 15:46.
|
|
|
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
Ch40zzC0d3r
Serious User
|
Serious User
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
MatAllum
OP
Senior Member
|
OP
Senior Member
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.
#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]
#460659
07/08/16 08:47
07/08/16 08:47
|
Joined: Oct 2011
Posts: 1,082 Germany
Ch40zzC0d3r
Serious User
|
Serious User
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:
#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
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
take a peek in your windows.h before importing already defined functions 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
|
|
|
|