Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel32 GetProcAddress for 64its #478

Closed
iR3SH opened this issue Aug 5, 2024 · 10 comments
Closed

Kernel32 GetProcAddress for 64its #478

iR3SH opened this issue Aug 5, 2024 · 10 comments

Comments

@iR3SH
Copy link

iR3SH commented Aug 5, 2024

Hi,
I'm trying to use the function GetProcAddress, when i run my application in 32Bits everthing works, but not when i launch it in Any CPU or 64Bits

Could you make the method compatible with 64Bits ?

Thanks :)

@dahall
Copy link
Owner

dahall commented Aug 6, 2024

Which overload are you using and what exception or result code are you getting?

@tajbender
Copy link
Contributor

tajbender commented Aug 6, 2024

I found something, maybe this is helpful:

StackOverflow: GetProcAddress function in C++

GetProcAddress function in C++

lpGetNumber = (LPGETNUMBER)GetProcAddress((HMODULE)hDLL, "GetNumber");

Checking return codes and calling GetLastError() will set you free. You should be checking return codes twice here. You are actually checking return codes zero times.

hDLL = LoadLibrary(L"MYDLL.DLL");

Check hDLL. Is it NULL? If so, call GetLastError() to find out why. It may be as simple as "File Not Found".

lpGetNumber = (LPGETNUMBER)GetProcAddress((HMODULE)hDLL, "GetNumber");

If lpGetNumber is NULL, call GetLastError(). It will tell you why the proc address could not be found. There are a few likely scenarios:

@tajbender
Copy link
Contributor

@iR3SH
Copy link
Author

iR3SH commented Aug 6, 2024

Thanks for all of your answer, i use it in a C# Library project, So first i Load my Library like this :
SafeHINSTANCE safeModule = LoadLibraryEx(DllUrl);
Then i want to load the functions of the dll like this GetProcAddress(safeModule, "CCAPIConnectConsole")
But then i look if any load of functions are loaded correclty

safeModule = LoadLibrary(DllUrl);
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIConnectConsole"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIDisconnectConsole"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetConnectionStatus"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetBootConsoleIds"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetConsoleIds"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetMemory"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetMemory"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetProcessList"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetProcessName"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetTemperature"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIShutdown"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIRingBuzzer"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPISetConsoleLed"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetFirmwareInfo"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIVshNotify"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetNumberOfConsoles"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetConsoleInfo"));
CCAPIFunctionsList.Add(GetProcAddress(safeModule, "CCAPIGetDllVersion"));

private bool IsCCAPILoaded()
        {
            for (int i = 0; i < CCAPIFunctionsList.Count; i++)
                if (CCAPIFunctionsList.ElementAt(i) == IntPtr.Zero)
                    return false;
            return true;
        }

And if i call GetLastError() i got the error code 0, in 64Bits i've an message who told me that the function is not found but in 32Bits it works well, so Every element on my list in 64Bits = IntPtr.Zero

@tajbender
Copy link
Contributor

tajbender commented Aug 6, 2024 via email

@iR3SH
Copy link
Author

iR3SH commented Aug 6, 2024

This was my bad, i'm tryng to load an 32Bits Library in an 64Bits tool, it can't work i think

@dahall
Copy link
Owner

dahall commented Aug 6, 2024

This was my bad, i'm tryng to load an 32Bits Library in an 64Bits tool, it can't work i think

https://stackoverflow.com/questions/2265023/load-32-bit-dll-library-in-64-bit-application

@dahall
Copy link
Owner

dahall commented Aug 6, 2024

Also, for functions like this that use a NULL handle to report failure, you can use Win32Error.ThrowLastErrorIfInvalid

CCAPIFunctionsList.Add(Win32Error.ThrowLastErrorIfInvalid(GetProcAddress(safeModule, "CCAPIGetDllVersion")));

@tajbender
Copy link
Contributor

tajbender commented Aug 7, 2024

Also, for functions like this that use a NULL handle to report failure, you can use Win32Error.ThrowLastErrorIfInvalid

CCAPIFunctionsList.Add(Win32Error.ThrowLastErrorIfInvalid(GetProcAddress(safeModule, "CCAPIGetDllVersion")));

@dahall: Another unrelated Question:

Does this always work?

Or does this only work on specific DLLs, like Shel32, Kernel32 etc.?

edit: How do I know, my specific call is supported by this approach?

@dahall
Copy link
Owner

dahall commented Sep 4, 2024

Any handle in Vanara that supports the IHandle interface or any IntPtr value will work with the Win32Error.ThrowLastErrorIfInvalid method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants