CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2006
    Posts
    15

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Memory violation in DLL test

    Quote Originally Posted by servalsoft View Post
    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

  3. #3
    Join Date
    Oct 2006
    Posts
    15

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    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

  5. #5
    Join Date
    Mar 2011
    Posts
    2

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

  6. #6
    Join Date
    Oct 2006
    Posts
    15

    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
  •  





Click Here to Expand Forum to Full Width

Featured