[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Reference C++ Header file with C#

Posted on 2006-07-11
17
Medium Priority
?
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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