tags:

views:

903

answers:

3

Hi,

I'm trying to call a 3rd party vendor's C DLL from vb.net 2005 and am getting P/Invoke errors. I'm successfully calling other methods but have hit a bottle-neck on one of the more complex. The structures involved are horrendous and in an attempt to simplify the troubleshooting I'd like to create a C++ DLL to replicate the problem.

Can somebody provide the smallest code snippet for a C++ DLL that can be called from .Net? I'm getting a "Unable to find entry point named XXX in DLL" error in my C++ dll. It should be simple to resolve but I'm not a C++ programmer.

I'd like to use a .net declaration for the DLL of

Declare Function Multiply Lib "C:\MyDll\Debug\MyDLL.DLL" Alias "Multiply" (ByVal ParOne As Integer, ByVal byvalParTwo As Integer) As Integer

+1  A: 

Try using the __decspec(dllexport) magic pixie dust in your C++ function declaration. This declaration sets up several things that you need to successfully export a function from a DLL. You may also need to use WINAPI or something similar:

__declspec(dllexport) WINAPI int Multiply(int p1, int p2)
{
    return p1 * p2;
}

The WINAPI sets up the function calling convention such that it's suitable for calling from a language such as VB.NET.

Greg Hewgill
A: 

You can try to look at the exported functions (through DumpBin or Dependency Walker) and see if the names are mangled.

On Freund
A: 

Using Greg's suggestion I found the following works. As mentioned I'm not a C++ programmer but just needed something practical.

myclass.cpp #include "stdafx.h"

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
     )
{
    return TRUE;
}

int _stdcall multiply(int x , int y)
{
    return x*y;
}

myclass.def LIBRARY myclass

EXPORTS

multiply @1

stdafx.cpp #include "stdafx.h"

stdafx.h

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_)
#define AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


// Insert your headers here
#define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers

#include <windows.h>


//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_)