C#调用dll文件,产生找不到程序入口点的问题
先用VC创建了一个dll的工程,代码如下:
//头文件
/*dlltry_04.h*/
#ifndef DLLTRY_04_
#define DLLTRY_04_
#ifdef EXPORT_SAMPLE
extern __declspec(dllexport) __stdcall int SampleMethod(int i);
#else
extern __declspec(dllimport) __stdcall int SampleMethod(int i);
#endif
//.cpp文件
//dlltry_04.cpp
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
int __declspec(dllexport) __stdcall SampleMethod(int i)
{
return i*10;
}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}
我用VC建立的测试工程测试了上述代码,没有问题。
在C#中,在调用上述的dlltry_04.dll文件发生了找不到程序入口点的错误
c#代码如下:
//C#工程
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//[DllImport("dllTry.dll")]
//private static extern void HelloWorld ();
[DllImport("DllTry_04.dll", EntryPoint = "?SampleMethod@@YAHH@Z")]
public static extern int SampleMethod(int x);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int i = 5;
Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(i));
}
}
}
程序入口点我是用vc工具dumpbin找到的,直接写在C#中。.cpp中的函数代码是MSDN提供的,因此也不可能出错。
求教啊!!!!
------解决方案--------------------
extern "C" __declspec(dllexport) __stdcall int SampleMethod(int i);