Access violation when loading function form dll

This is a discussion on Access violation when loading function form dll within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am attempting to make a function call from a dll file, the function in question is suppose ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    Access violation when loading function form dll

    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 to 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; 03-24-2011 at 03:40 PM. Reason: Clarification

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,306
    Check the return value from GetProcAddress(). It returns a NULL value if it fails - dereferencing that value (i.e. calling the function) will generally yield a crash.

    The first argument to GetProcAddress() would normally, in your code, be hDLL, not HMODULE(hMfdll) - whatever that is.

    Since your DLL source file is named with a .cpp extension, it will be compiled as C++ by default (depending on compiler settings though). If it is compiled as C++, the name will be mangled - which means its name as exported from the DLL is not as simple as "DisplayMsg". To prevent the mangling, declare it as extern "C".

    You also need to check the calling conventions match (although __stdcall is the default for VS).
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    There are multiple problems.

    1. What is hMfDll? You should be using hDLL.

    2. DLL exported functions must be declared extern "C", otherwise name mangling will bite you.

    3. It's good practice for DLL exports to be declared with __stdcall convention (actually, WINAPI, but it's the same thing).

    The default calling convention in Visual Studio is NOT __stdcall, it is __cdecl.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    @Grumpy, brewbuck
    Changing the default convention names, and adding the correct dll export to my dll function produced the expected output.
    However the main task I am trying to accomplish which is loading a particular window still doesnt work, I pretty confident there are not problems with that dll or its function because it has been working flawlessly for the last 10 years also, and all I am suppose to do is to port that particular function to windows 7, however when I make the function call, the application windows opens, but crashes straight away, I suspect there is a memory allocation problem, but I am not expert.
    Il gladly take any suggestions, and Ill post back if I get anything going. thanks again
    Yan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21