Click here to Skip to main content
10,500,000 members (91,342 online)
Rate this:
Please Sign up or sign in to 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)
#pragma once
#include "stdafx.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
typedef int (CALLBACK* PFunForExport)(LPCSTR,LPCSTR, LPCSTR);

in the c++ code i am loading the dll with LoadLibrary()
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

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

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 4:40am
Updated 20-Sep-12 2:24am
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.
Argonia 20-Sep-12 9:06am
Well the strange thing is when i tested the dll as an exe it worked just fine but now when it compiled as dll and called from native project it doesnt allow unmanaged function to call managed.
If you can provide me with similar example how this should be done will be great.All i can find is the other way around. A .net project is calling a mixed dll.
Richard MacCutchan 20-Sep-12 9:48am
I typed the question into Google and found this CodeProject article.
pasztorpisti 20-Sep-12 18:42pm
The way you load the DLL and handle GetProcAddress seems to be OK except that you shouldn't use the TEXT macro with GetProcAddress since that function has no widechar version. Since you code compiled successfully with GetProcaddress() + TEXT macro I assume that your exe project charset is asni. As previously mentioned the string casting can be dangerous, I would replace (char*) casts to static_cast<const char*>(), the latter should fail if your cast isn't valid because you want to cast from const wchar_t*. Put a breakpoint to the first line of your exported DLL function, does the first line get control?
Argonia 21-Sep-12 1:48am
Yes the dll gets control the problem is i cant manage to call the managed function. I have to export unmanaged function so my project can call it properly. I haven't look deeply the CodePrject article which was given from Richard MacCutchan but when i look it (yes i can google) i noticed that in both examples the first function which gets called is managed.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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 :
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):

I would suggest alloc memory for strLogFile get it's handle.(say ptrLogFile)
and then cast it's handle as (char *)ptrLogFile
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100