Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a third party DLL (including a Visual C++ Express project with source code) that is, I think, unmanaged. I Want to write a Managed C++ wrapper DLL so I can use the DLL from VB.

I have not touched C for about 15 years - most of my 'playing' has been in VB.

I have C++ and VB 2010 express.

I have created a new C++ Class Library and added some code to the header file (see below).

The code compiles. From VB I can see my property, but the two functions (WrapiguanaClose and WrapiguaneCode) are not visible. There will be several more properties and functions added once I can get the basics working.

It is probably a silly mistake - showing how out of date I am with C++.

Could anyone please tell me where I have gone wrong ?
C++
// iguanaIRDLLWrapper.h

#pragma once
// #pragma comment(lib, "iguanaIRDLLWrapper.lib")
#include "Stdafx.h"
#include "iguanaIR.h"
#include "resource.h"

using namespace System;
using namespace System::Reflection;
using namespace System::Windows::Forms;

namespace iguanaIRDLLWrapper {

	public ref class igWrap
	{
		// TODO: Add your methods for this class here.
public:
    property int Version
    {
        int get()
        {
            return 1;
        }
        
       /* void set(int x)
        {
            _StudRank = x;
        }*/
    }


	// void WrapiguanaIRClose(PIPE_PTR connection);
	// void WrapiguanaIRCode(const iguanaPacket pkt);
 
	public : void WrapiguanaClose(PIPE_PTR connection)
	    /*      void iguanaClose(PIPE_PTR connection)  */

{
	typedef int (__cdecl *igClosePROC)(HANDLE);

	HINSTANCE hinstLib; 
    igClosePROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
 
    // Get a handle to the DLL module.
 
    hinstLib = LoadLibrary(TEXT("iguanaIR.dll")); 
 
    // If the handle is valid, try to get the function address.
 
    if (hinstLib != NULL) 
    { 
        ProcAdd = (igClosePROC) GetProcAddress(hinstLib, "iguanaClose"); 
 
        // If the function address is valid, call the function.
 
        if (NULL != ProcAdd) 
        {
            fRunTimeLinkSuccess = TRUE;
            (ProcAdd) (connection); 
        }
        // Free the DLL module.
 
        fFreeResult = FreeLibrary(hinstLib); 
    } 
 }

	public : void WrapiguanaCode(const iguanaPacket pkt)
	    /*      unsigned char iguanaCode(const iguanaPacket pkt)  */

{
	typedef int (__cdecl *igCodePROC)(iguanaPacket);

	HINSTANCE hinstLib; 
    igCodePROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
 
    // Get a handle to the DLL module.
 
    hinstLib = LoadLibrary(TEXT("iguanaIR.dll")); 
 
    // If the handle is valid, try to get the function address.
 
    if (hinstLib != NULL) 
    { 
        ProcAdd = (igCodePROC) GetProcAddress(hinstLib, "iguanaClose"); 
 
        // If the function address is valid, call the function.
 
        if (NULL != ProcAdd) 
        {
            fRunTimeLinkSuccess = TRUE;
            (ProcAdd) (pkt); 
        }
        // Free the DLL module.
	}
}
	};
}

Thanks in advance for any help.

John Hadley
Posted
Updated 1-Jul-10 8:22am
v2
Comments
Sandeep Mewara 1-Jul-10 14:22pm    
Use 'pre' tags for formatting code part.

Hi,

I suppose it's because the types used as parameters of your methods are not exposed as managed data types.

V.
 
Share this answer
 
Err - sorry - I understand the words, but not the meaning.

I tried :-

// public : void WrapiguanaClose(PIPE_PTR connection)
public : void WrapiguanaClose(HANDLE connection)

with the same result.

If that was not your meaning what code should I include to expose my method parameters?

John Hadley
 
Share this answer
 
Comments
voloda2 2-Jul-10 3:21am    
I suppose that you must use ONLY managed data types (it means only types available from VB/C# managed code). So if you are using an unmanaged one (such as HANDLE, PIPE_PTR etc.) it will not work.

Based on definition of the PIPE_PTR you can use IntPtr managed type or an integral one (UInt32, UInt64).
Try using IntPtr instead than PIPE_PTR or HANDLE: the last two are datatypes defined only on C++

Another trick: avoid loading and unloading the DLL each time you make a call into it, it's a big waste of time. The best is to load the dll only once at the application startup and unload it at the end. You could export 2 functions from your wrapper dll to do it, like bool WrapperInitialize() and void WrapperUninitialize()
 
Share this answer
 
v2
Yes - it looks as if the parameter types were the problem.

IntPtr works HANDLE doesn't.

I will take your suggestion for loading the DLL - I was already planning an 'Open' and 'Close' function - so will load and unload there.

All I have to do now is find acceptable parameter types for my other Methods!


Thanks fpr your help.

John Hadley
 
Share this answer
 

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