Click here to Skip to main content
15,895,011 members

Calling a Mixed dll from c++ code

Revision 5
Hello everyone ,
I need your help again.
This time i cant manage to call exported function from mixed dll from c++ code
I will start from the function which i am trying to call
(This is my dll code)
C++
#pragma once
#include "stdafx.h"
#include<string.h>
#include <msclr/marshal.h>


using namespace System;
using namespace MyFSharpClass::EntryPointClass;
using namespace System::Runtime::InteropServices;
using namespace msclr::interop;


#pragma managed

void static EntryPointClassStart(char* strDebugLog ,char* strInputFile, char* strOutputFile)
{
	String^ stDebugLog = Marshal::PtrToStringAnsi(static_cast<IntPtr>(strDebugLog));
	String^ stInputFile = Marshal::PtrToStringAnsi(static_cast<IntPtr>(strInputFile));
	String^ stOutputFile = Marshal::PtrToStringAnsi(static_cast<IntPtr>(strOutputFile));

	EntryPointClass^ Text = gcnew EntryPointClass();
	Text->CalculateCoords(stDebugLog, false, stInputFile, stOutputFile);
}


#pragma unmanaged

extern "C" __declspec(dllexport)
int __stdcall FunForExport(char* const strDebugLog, char* const strInputFile, char* const strOutputFile)
{
	EntryPointClassStart(strDebugLog, strInputFile, strOutputFile);
	return 0;
}


The function pointer looks like this
C++
typedef int (CALLBACK* PFunForExport)(LPCSTR,LPCSTR, LPCSTR);

in the c++ code i am loading the dll with LoadLibrary()
C++
HMODULE hTheDll = LoadLibrary(TEXT(strdll)); //where the strdll keeps the full path of the dll and its name
...
PFunForExport pFunForExport = (PFunForExport)GetProcAddress(hTheDll, TEXT("_FunForExport@12"));

and when i call the pointer this way the program crashes
hTheDll is not null

C++
pFunForExport((char*)strLogFile.GetString(), (char*)strInputFile.GetString(),(char*)strOutputFile.GetString());


EntryPointClass is declared in F# program which dll is added in the reference of the mixed dll
The error which is shown when the program crashes is

Unhandled exception at 0x771115de in TheProgram.exe: 0xE0434352: 0xe0434352.

but in Output window i can see this

CSS
First-chance exception at 0x7632b9bc in See_View_Tester.exe: Microsoft C++ exception: EEFileLoadException at memory location 0x0241ccd8..
First-chance exception at 0x7632b9bc in TheExe.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x7632b9bc in TheExe.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x7632b9bc in TheExe.exe: 0xE0434352: 0xe0434352.
First-chance exception at 0x7632b9bc in TheExe.: Microsoft C++ exception: [rethrow] at memory location 0x00000000..


Now the pointer pFunForExport keeps _FunForExport(char*,char*,char*)

Please help me figure it out. Why its crashing and what exactly isn't right

P.S Visual Studio 2010 is my compiler
P.S 2
The .def file is removed.
I double checked the name of the function with Dependency Walker.
I added the improved dll code .I tested it and it works with simple main function and ran it as Exe .But it still crushes and i am not sure if i use the dll the right way .Or maybe the way i trying to pass the strings is wrong. Any idees are welcome.
P.S 3
I am pasting the code from Disassembly Window from the moment when i am trying to set the pointer to the function
05A974F3  mov         esi,esp  
05A974F5  push        offset string "_FunForExport@12" (5F9681Ch)  
05A974FA  mov         eax,dword ptr [ebp-0D8h]  
05A97500  push        eax  
05A97501  call        dword ptr [__imp__GetProcAddress@8 (605E4CCh)]  
05A97507  cmp         esi,esp  
05A97509  call        @ILT+36635(__RTC_CheckEsp) (59F4F20h)  
05A9750E  mov         dword ptr [ebp-0F0h],eax  


Too bad i don't understand assembly (never learned it) but when i checked my other function pointer to functions in other dlls i noticed that on the last mov in the brackets is the name of the pointer but not here.
Thank you in advance.
Posted 18-Sep-12 3:40am by Argonia.
Tags: , ,