<p>I'm trying to call a 3rd party vendor's C DLL from vb.net 2005 and am getting <code>P/Invoke</code> 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.  </p>

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

<p>I'd like to use a .net declaration for the DLL of</p>

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

[{'answer_id': 39079, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 2, 'selected': False, 'text': '<p>Try using the <a href="http://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx" rel="nofollow noreferrer">__decspec(dllexport)</a> 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:</p>\n\n<pre><code>__declspec(dllexport) WINAPI int Multiply(int p1, int p2)\n{\n    return p1 * p2;\n}\n</code></pre>\n\n<p>The WINAPI sets up the function calling convention such that it\'s suitable for calling from a language such as VB.NET.</p>\n'}, {'answer_id': 39118, 'author': 'On Freund', 'author_id': 2150, 'author_profile': 'https://Stackoverflow.com/users/2150', 'pm_score': 0, 'selected': False, 'text': '<p>You can try to look at the exported functions (through DumpBin or Dependency Walker) and see if the names are mangled.</p>\n'}, {'answer_id': 43629, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Using Greg\'s suggestion I found the following works.  As mentioned I\'m not a C++ programmer but just needed something practical.</p>\n\n<p><strong>myclass.cpp</strong>\n    #include "stdafx.h"</p>\n\n<pre><code>BOOL APIENTRY DllMain( HANDLE hModule, \n                       DWORD  ul_reason_for_call, \n                       LPVOID lpReserved\n                 )\n{\n    return TRUE;\n}\n\nint _stdcall multiply(int x , int y)\n{\n    return x*y;\n}\n</code></pre>\n\n<p><strong>myclass.def</strong>\n    LIBRARY myclass</p>\n\n<pre><code>EXPORTS\n\nmultiply @1\n</code></pre>\n\n<p><strong>stdafx.cpp</strong>\n    #include "stdafx.h"</p>\n\n<p><strong>stdafx.h</strong></p>\n\n<pre><code>// stdafx.h : include file for standard system include files,\n//  or project specific include files that are used frequently, but\n//      are changed infrequently\n//\n\n#if !defined(AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_)\n#define AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_\n\n#if _MSC_VER &gt; 1000\n#pragma once\n#endif // _MSC_VER &gt; 1000\n\n\n// Insert your headers here\n#define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers\n\n#include &lt;windows.h&gt;\n\n\n//{{AFX_INSERT_LOCATION}}\n// Microsoft Visual C++ will insert additional declarations immediately before the previous line.\n\n#endif // !defined(AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_)\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/39064', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']