[FIXED] Unable to import Win32 function (spoiler: I'm dumb)

Posted By: MatAllum

[FIXED] Unable to import Win32 function (spoiler: I'm dumb) - 07/03/16 15:39

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?
Posted By: Ch40zzC0d3r

Re: Can't import a Win32 API function - 07/03/16 15:45

Use LoadLibrary / GetModuleHandle and GetProcAddress.

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

Posted By: MatAllum

Re: Can't import a Win32 API function - 07/04/16 01:17

Looks like the perfect solution to me, but unfortunately I end up with the same problem still happening.
Posted By: Ch40zzC0d3r

Re: Can't import a Win32 API function - 07/04/16 22:54

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)
Posted By: MatAllum

Re: Can't import a Win32 API function - 07/08/16 01:49

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);
}

Posted By: Wjbender

Re: Can't import a Win32 API function - 07/08/16 08:42


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));
}

Posted By: Ch40zzC0d3r

Re: Can't import a Win32 API function - 07/08/16 08:47

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));
}

Posted By: Wjbender

Re: Can't import a Win32 API function - 07/08/16 09:00

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 .
Posted By: MatAllum

Re: Can't import a Win32 API function - 07/08/16 23:24

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.
© 2024 lite-C Forums