Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Updated 20-Sep-12 1:24am
v5
Comments
Richard MacCutchan 18-Sep-12 10:30am    
What is the return from strLogFile.GetString() (and the other calls)? Are you sure it is a normal C-style ASCII string?
Argonia 19-Sep-12 1:44am    
strLogFile (and the others) is CString variable and i need to transform it to char* to pass it. When i call the function pointer it gives the error right after the third call to GetString is completed
Richard MacCutchan 20-Sep-12 8:25am    
That could be any of lots of things wrong. Step through this code with your debugger and look at the actual values being returned from GetString() calls to see that they are valid.
Argonia 20-Sep-12 8:50am    
I just did and i saw that it steps into the dll function which is exported but when its called EntryPointClassStart it gives exception. I guess i didn't make the "unmanage code calling manage code" thing right.
Richard MacCutchan 20-Sep-12 8:58am    
I may be wrong, but I am not sure that you can step direct from unmanaged into managed code in that way.

1 solution

I see one problem...
On LoadLibrary you get hTheDll
and GetProcAddress you use something else GetProcAddress(hSeeView,...

I think you need to use same handle(Ptr) to get EntryPoint/method handle.

Also you may try :
LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010
LoadLibraryEx(path, PtrZero, LOAD_IGNORE_CODE_AUTHZ_LEVEL)


Also would like to suggest one more thing(this would be strange, but I use it when c# assembly for loading runtime c++ or c library and pass char * arguments:
You may try to modify it the line below(also similiar lines):
(char*)strLogFile.GetString()

I would suggest alloc memory for strLogFile get it's handle.(say ptrLogFile)
and then cast it's handle as (char *)ptrLogFile
 
Share this answer
 
v2
Comments
Argonia 18-Sep-12 10:08am    
Sorry, It's not this, i just made a mistake when i copied

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900