Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I've been looking into this with no avail. I've tried calling a function from a C++ dll (library) WITH an ENTRYPOINT, with no success. It's not an open source .dll, but their is a .NET library for it. I am currently trying to convert the C# library that calls the functions of the C++ .DLL into C++ CLI.
(So... C# library (using dllimport) conversion to C++CLI)
Of course DLLIMPORT doesn't work, and I've looked into
__declspec(dllexport)
&
__declspec(dllimport)
and I'm not getting anywhere. I have read on this to try and gain some clarity, but lost and still reading.

This is a working (C++ CLI) example for the older .DLL before it was updated... but this doesn't have an ENTRYPOINT in it, but the newer one does and i don't know how to add the entrypoint.

This is what the C++ CLI Version (without entrypoint (older version of DLL)) looks like:
XML
typedef int (__cdecl* Connectf)(const char*);
Connectf Connect  = (Connectf)GetProcAddress(LoadLibrary(L"CCAPI.DLL"),"connectConsole");


This is what the C# version of the LIB looks like:
C#
private static extern int connectConsole(string targetIP);
        [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi17disconnectConsoleEi", CallingConvention = CallingConvention.Cdecl)]


P.S.: Here's the C++ CLI Library one of my fellow programmers worked on, with little success: http://pastebin.com/0PTw73WZ

and the original C# Library (updated version): http://pastebin.com/ccATzTm8

Thanks for your time.

BaSs_HaXoR
Posted
Updated 17-Sep-14 18:43pm
v3
Comments
Sergey Alexandrovich Kryukov 18-Sep-14 1:22am    
You don't need to use P/Invoke with C++. But you can, in exact same way as with C#.
Or you can create a mixed-mode project and freely combine managed + unmanaged code.
—SA
BaSs_HaXoR 18-Sep-14 1:56am    
Yea, i thought about doing that, but figured that their should be an easier way. Just by calling the function. And also, what about the Entrypoint? How would i get past that?

Thanks for helping.
Sergey Alexandrovich Kryukov 18-Sep-14 2:46am    
"Just by calling what"? You first need to have the loaded DLL and get a function, either C++ or C++/CLI way. By the way, LoadLibrary with GetProcAddress will work for unmanaged code. (If you are sure the the DLL you want to use is native.)
I don't understand the problem of ENTRYPOINT. What is that? How can it be important?
—SA
BaSs_HaXoR 18-Sep-14 5:20am    
Yes, I'm positive the .dll is native. And an entry point is where control enters a program or piece of code. (wikied) And i know what has to be done, i just don't know how to do it. I know i have to load the dll and call the function... but don't know how to do that in CLI C++ with an Entrypoint. :/

1 solution

The code:
C++
typedef int (__cdecl* Connectf)(const char*);
Connectf Connect  = (Connectf)GetProcAddress(LoadLibrary(L"CCAPI.DLL"),"connectConsole");

shows you how to access the library. The LoadLibrary call loads the DLL into memory and the GetProcAddress call finds the entry point. However if either of these calls fail then it will not work. You should modify it something like:
C++
typedef int (__cdecl* Connectf)(const char*);
HMODULE hModule = LoadLibrary(L"CCAPI.DLL");
if (hModule == NULL)
{
    // throw an error
}
Connectf Connect  = (Connectf)GetProcAddress(hModule, L"connectConsole");
if (Connect == NULL)
{
    // throw an error
}

Then you will at least have some idea of why it is not working.
 
Share this answer
 
Comments
BaSs_HaXoR 18-Sep-14 5:14am    
It might have something to do with the .dll, but i can't even run it to get an exception... The program just force-closes/Breaks. I'm trying to do this in CLI C++, but the GetProcAddress doesn't allow the ability to set the Entrypoint... But you say it finds the Entrypoint? Because without the Entrypoint on the previous version this code works fine, but on the updated version of the library they're is an Entrypoint and forcecloses without it.

Idk. I've been working on this for hours on end. :|
Richard MacCutchan 18-Sep-14 5:24am    
Sorry I don't understand what you are saying. If the DLL has changed and you cannot find the entry point then you need to talk to the people who created it.

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