Link to home
Start Free TrialLog in
Avatar of tmark01
tmark01

asked on

VS 2008 - problem calling a C++ dll from VB.Net

We are trying to call C++ dll from a VB.Net application. Both projects are in VS 2008. We can succesfully call the dll from a C/C++ program, but not the VB.Net.

The message is:

"System error=Unable to find an entry point named 'fnRSIPackingLib' in DLL 'RSIPackingLib.dll'

Both projects are simple. Any help would be appreciated. Below is the source for both programs.

/****************************************************************/
// RSIPackingLib.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "RSIPackingLib.h"

// This is an example of an exported variable
RSIPACKINGLIB_API int nRSIPackingLib=0;

// This is an example of an exported function.
RSIPACKINGLIB_API int fnRSIPackingLib(int *tVal)
{
      *tVal = 299;
      return 42;
}

// This is the constructor of a class that has been exported.
// see RSIPackingLib.h for the class definition
CRSIPackingLib::CRSIPackingLib()
{
      return;
}
/****************************************************************/
RSIPackingLib.h

#ifdef RSIPACKINGLIB_EXPORTS
#define RSIPACKINGLIB_API __declspec(dllexport)
#else
#define RSIPACKINGLIB_API __declspec(dllimport)
#endif

// This class is exported from the RSIPackingLib.dll
class RSIPACKINGLIB_API CRSIPackingLib {
public:
      CRSIPackingLib(void);
      // TODO: add your methods here.
};

extern RSIPACKINGLIB_API int nRSIPackingLib;

RSIPACKINGLIB_API int fnRSIPackingLib(int *tVal);

/***************************************************************/
RSIPackingLib.def

LIBRARY RSIBackingLib
DESCRIPTION 'A C++ dll that can be called from VB'

EXPORTS
    fnRSIPackingLib

/****************************************************************/

VB.Net code

Imports System.Runtime.InteropServices
Public Class Form1
    <DllImport("RSIPackingLib.dll", CallingConvention:=CallingConvention.Cdecl)> _
    Private Shared Sub fnRSIPackingLib(ByRef stan As Integer)
    End Sub

    Private Sub DisplayErrMsg(ByVal msg As String)
        Call MsgBox("System error=" & msg, MsgBoxStyle.Critical, "Simple Test")
    End Sub

    Private Sub BtnSimple_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSimple.Click
        Dim Num As Integer = 8
        Try
            fnRSIPackingLib(Num)
            MessageBox.Show("Num:" & Num)
        Catch ex As Exception
            Call DisplayErrMsg(Err.Description)
        End Try
    End Sub
End Class
Avatar of alexey_gusev
alexey_gusev
Flag of United Kingdom of Great Britain and Northern Ireland image

hmm, how about "extern "C" {}" declarations?
the easiest way to verify it is to open your dll in Dependency Walker and see what it says about exported functions.
Avatar of tmark01
tmark01

ASKER

Thank you. I will try this. I am out of the office today, but will try it first thing tomorrow AM (8/17/12).
ASKER CERTIFIED SOLUTION
Avatar of alexey_gusev
alexey_gusev
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tmark01

ASKER

The exertn C did solve the problem. Thanks much for your help