We help IT Professionals succeed at work.
Get Started

Calling Unmanaged C++ code in VB.NET

Last Modified: 2013-11-06
I can trying to call a DLL written in unmanaged C++ from VB.NET. This DLL was given to me along with the .h file for the function I am trying to call.

The .h file for the DLL is enclosed.

I know how to import the DLL using DLL import. We have declared
    Structure TLData
        Public MyTypeAs Integer
        Public Expire As Integer 'in seconds
        Public SN As Integer
        Public Data() As Char
    End Structure

    Public Structure TLKey
        Public Key As String
        Public SNLen As Integer
        Public SerialNumber As String
    End Structure

    <DllImport("LicenseGen.dll")> _
    Public Shared Function CreateKey(ByRef key As TLKey, ByVal Data As TLData) As Integer
    End Function

But we get errors when we try to call the CreateKey function :
System.Runtime.InteropServices.SafeArrayTypeMismatchException: Specified array was not of the expected type.
   at MyVBProg.MyClass.CreateKey(TLKey& key, TLData Data)
   at MyVBProg.MyClass.NCKey() in C:\Documents and Settings\fcfang\My Documents\Visual Studio 2005\Projects\MyVBProg\MyVBApp\CallIt.vb:line 49

and line 49 is where I call the CreateKey function.

Sorry for the length of the post. Trying to make it as complete as possible.

Please and thanks in advance for any responses.

the DLL's .h file:
#pragma once
namespace MyLicGen {
#ifdef __cplusplus 
extern "C" {
#include <time.h>
typedef enum {
  leNoError = 0,
  leInvalidCharacter = 1,
  leValidityCheckFail = 2, 
  leForged = 3 
} TLError;
typedef struct {
  int SNLength;
  unsigned char SN[MAX_SERIAL_LENGTH];
} TLKey;
typedef enum {
  ltHardware = 1, 
  ltSoftware = 2,
  ltDriver = 3
} TLType;
typedef enum {
  lfMyFeature = 1, 
  lfMyOtherFeature = 2,
  lfMy3rdFeature = 3
} TLFeature;
typedef struct {
  TLType Type;
  time_t ExpiresAt;
  int SerialNumber; 
  unsigned char Data[2]; 
} TLData;
TLError CreateKey(TLKey *Key, TLData *Data);
#ifdef __cplusplus 

Open in new window

Watch Question
This problem has been solved!
Unlock 3 Answers and 8 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE