[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 535
  • Last Modified:

Reference C++ Header file with C#

I have a third party API that I am trying to connect to.  I am using C# and I need to reference the already compiles .DLL's and .LIB and .H files from C++.

I'm new to the Visual Studio .Net environment so I need to know how to reference the structure of the .Header files so I can create a .DLL that will connect to the APIs.

The purpose of this is to Create a DLL that will allow me to access from Visual Basic 6.0 and Visual Basic .Net.  I already successfully created the C# DLL and can reference functions on the C#DLL.  I just need to get the C# .DLL to connect to the API's and I cannot seem to figure out how to get it set up propertly.

Any help would be great...
0
mre531s
Asked:
mre531s
  • 12
  • 5
1 Solution
 
AlexFMCommented:
If C++ Dll exports functions, you can call them directly using PInvoke. If C++ Dll exports classes, it cannot be used by C# client. In this case you need to create C++/CLI Class Library wrapper which calls unmanaged C++ code and exposes .NET interface to C# client.
Having C++ h and lib files, you can add them to C++/CLI Class Library project, exactly like it is done in unmanaged C++ project. Unmanaged code can be called directly from C++/CLI.
0
 
mre531sAuthor Commented:
Would you be able to supply me a simple code snipped on how to create the Header and .cpp file so it an be used for exporting the funtions?
0
 
mre531sAuthor Commented:
this is what i have for a Header file


#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                               )

#ifdef _MANAGED
#pragma managed(pop)
#endif

#define DllImport   __declspec( dllimport )
#define DllExport   __declspec( dllexport )

extern DllExport int TestReturn();



the TestReturn Function is the function that I want to be able to call from VB....
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mre531sAuthor Commented:
Here is what I have for the .cpp file


#include "stdafx.h"



BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                               )
{
    return TRUE;
}

extern int TestReturn()
{
      return 1;
}
0
 
AlexFMCommented:
You said that you already have C++ project and need to use it in C#. What to you want to do exactly - write C++ project or use ready C++ code in C#?
0
 
mre531sAuthor Commented:
I keep getting a "Cannot find DLL entry point" when I try to make the call from vb.  Here is my VB Snippet....

Private Declare Function TestReturn Lib "CPSClient_Wrapper.dll" Alias "TestReturnA" () As Long
Private Sub Command1_Click()
    MsgBox TestReturn
End Sub
0
 
mre531sAuthor Commented:
I need to call the API's and I was trying to do it in C#, but I think that I will have to do it in C++ .NET...   My end goal is to create a .DLL that I can call from VB so i can interface with the API's...
0
 
mre531sAuthor Commented:
I'm Creating a Wrapper that will "Wrap" around the API's files and then I can use that Wrapper to call all the needed functions.  

I will then use VB to interface with my "Wrapper"...
0
 
AlexFMCommented:
Try this in Dll:

extern "C"
int TestReturn()
{
     return 1;
}

In VB:
Private Declare Function TestReturn Lib "CPSClient_Wrapper.dll" As Integer

Use dumpbin utility to see list of functions exported from Dll.
0
 
mre531sAuthor Commented:
I've never used the dumpbin utility.  how do you do that?
0
 
mre531sAuthor Commented:
I got it working.  However, when I modify my function to accept a parameter, I get an error  "Bad DLL Calling Convention".

I've made sure my types are correct and that my Function Signature in my Header File matches that of the one in my .CPP file.

any ideas....
0
 
AlexFMCommented:
dumpbin /exports CPSClient_Wrapper.dll

You can find dumpbin.exe file in one of VS subdirectories.
0
 
AlexFMCommented:
Show modified Dll code and VB .NET declaration.
0
 
mre531sAuthor Commented:
Here is the header file

// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the TEMP_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// TEMP_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef CPSCLIENT_WRAPPER_EXPORTS
#define CPSCLIENT_WRAPPER_API __declspec(dllexport)
#else
#define CPSCLIENT_WRAPPER_API __declspec(dllimport)
#endif

// This class is exported from the temp.dll
class CPSCLIENT_WRAPPER_API CCpsClientWrapper {
public:
      int CCPSCLIENT_WRAPPER(void);
      // TODO: add your methods here.
};

extern CPSCLIENT_WRAPPER_API int ncpsclient_wrapper;


CPSCLIENT_WRAPPER_API int TempReturn(int);
0
 
mre531sAuthor Commented:
Here is the .CPP file


// CpsClient_Wrapper.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "CPSClient_Wrapper.h"


#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                               )
{
      switch (ul_reason_for_call)
      {
      case DLL_PROCESS_ATTACH:
      case DLL_THREAD_ATTACH:
      case DLL_THREAD_DETACH:
      case DLL_PROCESS_DETACH:
            break;
      }
    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

// This is an example of an exported variable
CPSCLIENT_WRAPPER_API int ntemp=0;

// This is an example of an exported function.
CPSCLIENT_WRAPPER_API int TempReturn(int x)
{
      int myVal;

      myVal = 42 + x;

      return myVal;
}




0
 
mre531sAuthor Commented:
Here is the VB code....I'm having a problem with the "Alias"  I'm having to look into the .MAP file to find the compiled name the function gets.  Otherwize it doesn't find the entry point.  This is the most recent name ""?TempReturn@@YAHH@Z".   would the Extern "C" take care of the problem?

Private Declare Function TempReturn Lib "CPSClient_Wrapper.dll" Alias "?TempReturn@@YAHH@Z" (ByVal myNum As Long) As Long

Private Sub Command1_Click()
    MsgBox TempReturn(1)
End Su
0
 
mre531sAuthor Commented:
I figured it out..  The function had to be declared as such...

CPSCLIENT_WRAPPER_API int _stdcall TempReturn(IN int myNum);
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now