JeffRozar

Here is some code I wrote that needs to be called from a Win32 exe. The Win32 exe needs to run on both 9x and NT platforms. The code compiles with no problem on VS 2005, but the DLL will not load when run in 9x.

It runs under NT, and only if I uncomment the "//return" line, will it run under 9x. LsaOpenPolicy is an NT platform API - it's as though the DLL is calling or referencing the LsaOpenPolicy even when blocked by an (if) statement on 9x. Is it something obvious I'm missing I dont' want to create two DLLs, one for 9x and another for NT.


************************* DLL (host.dll) *************************

#include <windows.h>
#include <ntsecapi.h>

NTSTATUS GetLsa();
NTSTATUS ntsTemp;
BOOL bIsNTPlatform;

...

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
{
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if (!GetVersionEx((OSVERSIONINFO *) &osvi))
{
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (!GetVersionEx((OSVERSIONINFO *)&osvi))
return FALSE;
}

bIsNTPlatform = (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT);
if (bIsNTPlatform)
ntsTemp = GetLsa();

return TRUE;
}

NTSTATUS GetLsa()
{
LSA_HANDLE hPolicy = LSA_HANDLE(0);
LSA_OBJECT_ATTRIBUTES ObjectAttributes;
//return 1;
return LsaOpenPolicy(NULL, &ObjectAttributes, POLICY_VIEW_LOCAL_INFORMATION, &hPolicy);
}

************************* DLL (host.dll) *************************






************************* Simple VB EXE that calls DLL *************************

Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Sub Main()

MsgBox LoadLibrary(App.Path & "\host.dll"), , "LoadLibrary")

End Sub

*************************
Simple VB EXE that calls DLL *************************


Re: Visual C++ Language DLL fails on 9x platform, runs on NT platform

Marius Bancila

LsaOpenPolicy is an NT platform API - it's as though the DLL is calling or referencing the LsaOpenPolicy even when blocked by an (if) statement on 9x.

Did you try to debug you code If not I suggest so as a first step to solve the problem.






Re: Visual C++ Language DLL fails on 9x platform, runs on NT platform

JeffRozar

I debug _all day_, lol.




Re: Visual C++ Language DLL fails on 9x platform, runs on NT platform

Marius Bancila

And Does the code execute even when running on Win9x




Re: Visual C++ Language DLL fails on 9x platform, runs on NT platform

Marius Bancila

You are using OSVERSIONINFOEX. This structure requires Windows XP, Windows 2000 Professional, or Windows NT Workstation 4.0 SP6 and later. For Win9x you have to use OSVERSIONINFO.




Re: Visual C++ Language DLL fails on 9x platform, runs on NT platform

JeffRozar

Yes it runs, but only if I uncomment the "//return" line, will it run under 9x. The DLL loads on 9x as long as there is a "return" before the NT-only API call.

First I try with OSVERSIONINFOEX, then if that fails (as it will on 9x platforms), it runs OSVERSIONINFO. Through debugging, this is working fine and does return the correct GetVersionEx information.




Re: Visual C++ Language DLL fails on 9x platform, runs on NT platform

Marius Bancila

From what you're saying I understand that GetLsa() gets called whether you run on NT or 9x, right




Re: Visual C++ Language DLL fails on 9x platform, runs on NT platform

JeffRozar

No, GetLsa() is only called if it's run on NT. However, the DLL will only load on 9x.*if* the "//return" line is uncommented.




Re: Visual C++ Language DLL fails on 9x platform, runs on NT platform

JeffRozar

The solution is to either dynamically link the functions at runtime instead of linking the functions into the DLL at link time (shown below), OR add the advapi32.dll to the "Delay Loaded DLLs" under Linker/Input.

typedef NTSTATUS (NTAPI *PLSAOPENPOLICY)(PLSA_UNICODE_STRING, PLSA_OBJECT_ATTRIBUTES, ACCESS_MASK, PLSA_HANDLE);
PLSAOPENPOLICY pLsaOpenPolicy = NULL;

h32 = LoadLibrary(TEXT("advapi32.dll"));
if (h32)
{
pLsaOpenPolicy = (PLSAOPENPOLICY) GetProcAddress(hAdvApi32,"LsaOpenPolicy");

pLsaOpenPolicy(NULL,&ObjectAttributes,POLICY_VIEW_LOCAL_INFORMATION,&hPolicy);
}