Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a native library written as below.
Hello.h:
C++
#include<iostream>
using namespace std;


class __declspec(dllexport) Hello
{
public:	
	int PrintHello(char* str);
};


Hello.cpp
C++
#include<iostream>
#include "Hello.h"
using namespace std;

int Hello::PrintHello(char* str)
{
    cout<<"Hello "<< str <<endl;
    return 0;
}


then wrote some Exposing function

API.h

C++
#include<iostream>
#include"Hello.h"
__declspec(dllexport) Hello* APICreateHello();
__declspec(dllexport) void APIDestroyHello();


API.cpp:
C#
#include<iostream>
#include "API.h"

extern "C" __declspec(dllexport)  Hello*  __cdecl APICreateHello(char* str)
{
    return new Hello();
}
extern "C" __declspec(dllexport)  void   APIDestroyHello(Hello* obj)
{
        delete obj;
}

Dumpbin results in APICreateHello and APIDestroyHello as it is. there is no Name mangling.

Tried accessing using C# main as below but results in EntryPointException.

C#
public class MyCustomDLLWrapper
{
    // extern "C" MYCUSTOMDLL_API int AddNumbers(int x, int y);
    [DllImport("TestLib.dll",EntryPoint = "APICreateHello")]
    public static extern IntPtr APICreateHello(string str);
}
namespace TestManagedApp
{
    class Program
    {        public static int Main(string[] args)
        {
            IntPtr x = MyCustomDLLWrapper.APICreateHello("Pavan");


            return 0;
        }
    }
}


Please help. Thanks in advance
Posted
Comments
Richard MacCutchan 10-Jul-13 10:54am    
I think you should use __stdcall rather than __cdecl in your DLL source code.
Sergey Alexandrovich Kryukov 10-Jul-13 10:57am    
Why do you use __cdecl? It could be a problem. Use only dllexport.
As to name mangling, it's not a real problem.
—SA
PavanGoutami 12-Jul-13 1:06am    
Thanks for suggestions...Even after compiling the __stdcall also .. i am facing same error in Managed App. Am i missing some thing in the Managed code in context of EntryPoint..please help
Richard MacCutchan 12-Jul-13 2:57am    
What error? As I stated below, I changed your code to use __stdcall and it works fine, although it does not do anything.
PavanGoutami 16-Jul-13 4:06am    
Richard,
I compiled the code with __stdcall but am facing the same EntrypointException....is my Managed client correct? If it needs any changes. Please suggest me.

1 solution

I have built these projects using __stdcall as noted above, and it works, as far as it goes. I leave you to work on the remaining issues.
 
Share this answer
 
Comments
The_Inventor 11-Jul-13 3:44am    
Is the API.cpp considered to be part of the .dll source code?
Richard MacCutchan 11-Jul-13 4:04am    
Obviously yes, although its purpose is somewhat obscure.
The_Inventor 11-Jul-13 4:14am    
So the API.dll is the 'executable'?
Richard MacCutchan 11-Jul-13 4:18am    
No, a DLL is a shared library that is accessed by executable applications (.exe files). The executable is the C# code in the last block of source code.
The_Inventor 11-Jul-13 4:28am    
As I thought, thus the '', then how, or what kind, of extension is the API file pair going to create? I am presuming that Hello.cpp and Hello.h create the DLL as described above.

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