Solved

Reference C++ Header file with C#

Posted on 2006-07-11
17
446 Views
Last Modified: 2008-03-03
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
Comment
Question by:mre531s
  • 12
  • 5
17 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17082753
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
 

Author Comment

by:mre531s
ID: 17083377
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
 

Author Comment

by:mre531s
ID: 17083383
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
 

Author Comment

by:mre531s
ID: 17083390
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17083392
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
 

Author Comment

by:mre531s
ID: 17083398
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
 

Author Comment

by:mre531s
ID: 17083412
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
 

Author Comment

by:mre531s
ID: 17083421
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 48

Expert Comment

by:AlexFM
ID: 17083487
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
 

Author Comment

by:mre531s
ID: 17083583
I've never used the dumpbin utility.  how do you do that?
0
 

Author Comment

by:mre531s
ID: 17083853
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17083876
dumpbin /exports CPSClient_Wrapper.dll

You can find dumpbin.exe file in one of VS subdirectories.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17083886
Show modified Dll code and VB .NET declaration.
0
 

Author Comment

by:mre531s
ID: 17083924
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
 

Author Comment

by:mre531s
ID: 17083927
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
 

Author Comment

by:mre531s
ID: 17083943
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
 

Author Comment

by:mre531s
ID: 17084466
I figured it out..  The function had to be declared as such...

CPSCLIENT_WRAPPER_API int _stdcall TempReturn(IN int myNum);
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now