-
March 24th, 2011, 04:39 AM
#1
Memory violation in DLL test
Hello everyone,
I am attempting to make a function call from a dll file, the function in question is suppose to load a separate window. However when I make the call I the windows is loaded(I can see the GUI), but crashes straight away.
I therefore attempted to write a small sample app with a function of same return type and number of parameters as the one I am suppose to be calling, just for testing purposes, however I am running into a memory access violation error while trying to implement the sample app to execute a function from a dll; leading me to conclude that there is a fundamental mistake in my code.
I have two source files,
File 1: LibTest.cpp which is as follow
Code:
// LibTest.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <Windows.h>
void DisplayMsg(){
MessageBox(0, L"Hello from test lib", L"Test Lib", MB_OK);
}
With file one I was able to create a dll, please note that I must use dynamic loading since the ".lib" file is not available for my final task. Once the dll created, I created a second sample app to attempt to call the function above, here is the code of the procedure making the call:
Code:
#include <windows.h>
void LoadMfKbdDll() {
//Loading the library
HINSTANCE hDLL = LoadLibrary(TEXT("LibTest.dll"));
if (hDLL != NULL){
MessageBox(0, L"Test Lib Loaded", L"Test Lib", MB_OK);
//Get The point to the Display Message function in the dll
typedef void (__stdcall* pICFUNC)(); //building a template for the DisplayMsg function
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hDLL),"DisplayMsg");
pICFUNC DisplayMsg;
DisplayMsg = pICFUNC(lpfnGetProcessID);
DisplayMsg(); //calling the function
FreeLibrary(hDLL); //releasing the dll
}
else {
MessageBox(0, L"Error Loading libTest.dll", L"Login UI", MB_OK);
}
}
Now my problem is every time I attempt to run the file, I run into a memory access violation, I tried to debug as much as I could but I have now hit a solid wall, any help or suggestions would be greatly appreciated. Thanks
Yan
Note:
1) I am using VS 2010, SP1 on a Win7 x64 SP1 machine.
2) I have posted this question in another forum also, I hope it is not against regulations.
Last edited by servalsoft; March 24th, 2011 at 04:40 PM.
Reason: Clarification + Correction in code
-
March 24th, 2011, 07:26 AM
#2
Re: Memory violation in DLL test
Originally Posted by servalsoft
Code:
#include <windows.h>
void LoadMfKbdDll() {
//Loading the library
HINSTANCE hDLL = LoadLibrary(TEXT("LibTest.dll"));
if (hDLL != NULL){
MessageBox(0, L"Test Lib Loaded", L"Test Lib", MB_OK);
//Get The point to the Display Message function in the dll
typedef void (__stdcall* pICFUNC)(); //building a template for the DisplayMsg function
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hMfDll),"DisplayMsg");
...
}
}
The handle of the loaded "LibTest.dll" is hDLL .
But you are passing some other value (hMfDll) into GetProcAddress call. Is what you want?
Besides, you must always check the return values of all API calls including this GetProcAddress!
Victor Nijegorodov
-
March 24th, 2011, 04:24 PM
#3
Re: Memory violation in DLL test
Hello,
Thank you for taking the time for looking at my post, I copied the code wrong, but I have edited it now. I also understand I must always check the return type of my API function, but I just assume checking that the library was successfully loaded was good enough for me to go on. I more or less applied directly from the sample on the Microsoft website, because I have only just been exposed to this.
Thanks again for your input.
Yan
...Don't be afraid, only believe.
-
March 24th, 2011, 04:28 PM
#4
Re: Memory violation in DLL test
The fact "the library was successfully loaded" does NOT mean this library does contain the function you are trying to use!
Victor Nijegorodov
-
March 24th, 2011, 04:29 PM
#5
Re: Memory violation in DLL test
There are two main problems:
First is that the default calling convention in Visual Studio is __cdecl and not __stdcall if you didnt specify it else.
The second one is that in order to have your function exported you need to specify that. Thus in the DLL it will be:
Code:
#include "stdafx.h"
#include <Windows.h>
extern "C" __declspec(dllexport) void DisplayMsg(){
MessageBox(0, L"Hello from test lib", L"Test Lib", MB_OK);
}
-
March 24th, 2011, 05:49 PM
#6
Re: Memory violation in DLL test
@VictorN
You are absolutely right, I must teach myself to respect as much as possible those safe practices.
@Yanicks
Thanks for your help, your input did it for me, and the test app is working properly now and displays the message as expected; unfortunately for me my main one isn't, and right this minute I don't have access to the source code of the dll to check whether or not the "dll export" was set properly, although I am almost certain it was, since the dll has been used properly up until now, the problem is now I have to port one of its functionality to windows 7 and when I call the needed function, it does load the windows it is supposed to, but crashes directly; so I suspect there is still a problem in my code, my guess is something to do with memory allocation but what do I know, Im not so sure.
I will post back when I get more details.
Thanks again to both of you for taking the time to help me.
Yan
...Don't be afraid, only believe.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|