Jump to content
  • Advertisement
Sign in to follow this  
b1gjo3

dll help

This topic is 3743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my exe program, I use LoadLibrary to load my dll and then i get the pointers to these functions. I then call the functino StartupFunc("C:\\test.txt") and it copies the parameter to the global filepath variable. Then when i call somefunc, the filepath is always null. What am i doing wrong? .dll file
char*		filepath = NULL;
bool		shiftState = false;

extern "C" __declspec (dllexport) int __stdcall SomeFunc()
{
	fopen_s(&file1, filepath, "a+");
}

extern "C" int __declspec (dllexport) __stdcall StartupFunc(char* fpath)
{
        filepath = new char[strlen(fpath) + 1];
        strcpy_s(filepath, strlen(fpath) + 2, fpath);
	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Set up a watchpoint (look that up on google if you don't know what it is) on the address of "filepath" and you will find out when (and therefore, most likely, why) "filepath" ends up being null.

Share this post


Link to post
Share on other sites
based on the code snippet, does it look like something is wrong? im new to dll's so therefore i dont know how they work 100% yet. ill try to set a "watchpoint" on the address of filepath

Share this post


Link to post
Share on other sites
I cant seem to find where or why filepath changes back to null. After its set and the function returns, the variable is set to null and i dont know why..

Share this post


Link to post
Share on other sites
Post your real code then, because example you have posted (which I assume is a simplified example since it doesn't compile as-is) works OK for me.

This is my dll code

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

char* filepath = NULL;
bool shiftState = false;

extern "C" __declspec (dllexport) int __stdcall SomeFunc()
{
printf(filepath); // outputs "blah blah"
return 0;
}

extern "C" int __declspec (dllexport) __stdcall StartupFunc(char* fpath)
{
filepath = new char[strlen(fpath) + 1];
strcpy_s(filepath, strlen(fpath) + 2, fpath);
return 0;
}


this is my main code:

int _tmain(int argv, const char** argc)
{
HMODULE h = LoadLibrary(L"test.dll");

typedef int (__stdcall *F1)();
typedef int (__stdcall *F2)(char*);

F1 f1 = (F1) GetProcAddress(h, "_SomeFunc@0");
F2 f2 = (F2) GetProcAddress(h, "_StartupFunc@4");

f2("blah blah");
f1();

}


And what does the call stack look like when your watch point gets triggered?

Share this post


Link to post
Share on other sites
.dll

#include <windows.h>
#include <fstream>

HINSTANCE hInst = NULL;
HHOOK keyHook = NULL;
int keyCount = 0;
FILE* file1 = NULL;
char* filepath = NULL;
int hookState = -1;

extern "C" __declspec (dllexport) int _stdcall GetHookState()
{
return hookState;
}

extern "C" __declspec (dllexport) int _stdcall AppendToLog(char* append)
{
fopen_s(&file1, filepath, "a+");

if (file1 == NULL)
MessageBox(NULL, TEXT("Failed To Open File"), TEXT("I/O Error"), MB_OK);

fwrite(append, 1, strlen(append) + 1, file1);

return 0;
}

extern "C" __declspec (dllexport) LRESULT CALLBACK __stdcall KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
if ( (code < 0) || (wParam == VK_CONTROL) || (wParam >= VK_F1 && wParam <= VK_F12))
return CallNextHookEx(keyHook, code, wParam, lParam);

if (code == HC_ACTION)
{
fopen_s(&file1, filepath, "a+");

if (file1 == NULL)
MessageBox(NULL, TEXT("Failed To Open File"), TEXT("I/O Error"), MB_OK);

//only catch key up events
if ((HIWORD(lParam) & KF_UP) == 0)
count++;

//...
//write key count to file
//...

fclose(file1);
}//end if (code == HC_ACTION)
return CallNextHookEx(keyHook, code, wParam, lParam);
}

extern "C" int __declspec (dllexport) __stdcall SetKeyboardHook(char* fpath)
{
//set the keyboard hook
keyHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hInst, 0);

if (keyHook == NULL)
{
MessageBox(NULL, TEXT("Could Not Set Hook"), TEXT("Error #003"), NULL);
hookState = -1;
return -1;
}
else
{
filepath = new char[strlen(fpath) + 1];
strcpy_s(filepath, strlen(fpath) + 2, fpath);
MessageBox(NULL,TEXT("Hooked"),TEXT("Attached"),MB_OK);
hookState = 0;
return 0;
}
}

extern "C" int __declspec (dllexport) __stdcall UnsetKeyboardHook()
{
//unhook
UnhookWindowsHookEx(keyHook);

if (filepath != NULL)
delete [] filepath;

hookState = -1;
MessageBox(NULL,TEXT("Unhooked"),TEXT("Detached"),MB_OK);
return 0;
}

extern "C" BOOL __declspec (dllexport) __stdcall DllMain (HINSTANCE hInstance, DWORD reason, LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
{
//save instance
hInst = hInstance;
break;
}
case DLL_PROCESS_DETACH:
{
return 0;
break;
}
}
return TRUE;
}




main

#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

char* logPath = {"C:\\Users\\BigJoe\\Documents\\AFB Work\\count.txt"};

typedef int (__stdcall *SetKybdHk) (char* fpath);
typedef int (__stdcall *ApndToLg) (char* append);
typedef int (__stdcall *IntFunc) ();
typedef char (__stdcall *GetLastKey) ();

int main()
{
//get the hook library for the keyboard
HINSTANCE hDll = LoadLibrary("KeyProDll.dll");
if (hDll == NULL)
{
MessageBox(NULL, TEXT("Could Not Load Dll"), TEXT("Error #001"), MB_OK);
return 0;
}

SetKybdHk SetKeyboardHook = reinterpret_cast<SetKybdHk>(GetProcAddress(hDll, "_SetKeyboardHook@4"));
ApndToLg AppendToLog = reinterpret_cast<ApndToLg>(GetProcAddress(hDll, "__AppendToLog@4"));
IntFunc UnsetKeyboardHook = reinterpret_cast<IntFunc>(GetProcAddress(hDll, "_UnsetKeyboardHook@0"));
IntFunc GetHookState = reinterpret_cast<IntFunc>(GetProcAddress(hDll, "_GetHookState@0"));

SetKeyboardHook(logPath);

while (GetHookState() == 0) {}

UnsetKeyboardHook();;
FreeLibrary(hDll);
system("pause");
return 0;
}


Share this post


Link to post
Share on other sites
Nothing wrong here, any dll function I call after SetKeyboardHook has been called is able to see the correct value of filepath. The problem does not seem to be in the code you posted.

Share this post


Link to post
Share on other sites
thats the only code that I have and the only thing commented out was the conversion from int to char to output to file. maybe its something set in my project settings

Share this post


Link to post
Share on other sites
ok.. i have no idea why, but its working now and i didn't change a thing. i should probably blame this on msvc++

---edit

ok so after a long day of work, i have found something out. the filepath variable is not being set to null like I thought it was, fopen is failing when the variable is specified.

msvc++ comes up with the exception in fopen.c line: 54, expression: (file != NULL)

so it seems to me that my file is already opened. however, when i specify a constant char array in place of the filepath variable, the function succeeds and my program runs fine. any suggestions/ideas on why this is?

[Edited by - b1gjo3 on June 26, 2008 8:45:31 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!