• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

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
0
tmark01
Asked:
tmark01
  • 2
  • 2
1 Solution
 
alexey_gusevCommented:
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.
0
 
tmark01Author Commented:
Thank you. I will try this. I am out of the office today, but will try it first thing tomorrow AM (8/17/12).
0
 
alexey_gusevCommented:
I bet it is all about function names decoration. I've seen sometimes that it's not enough to just define the function in DEF file, it gets mungled anyway, so 'extern "C" ' could help
0
 
tmark01Author Commented:
The exertn C did solve the problem. Thanks much for your help
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now