【发布时间】:2013-02-28 20:24:11
【问题描述】:
我想从 C# 中调用方法“Talk”。我浏览了其他相关帖子,但对我没有帮助。
Managed.Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Managed
{
class Program
{
[DllImport("Unmanaged.exe", CallingConvention=CallingConvention.Cdecl,EntryPoint="Talk",CharSet=CharSet.Ansi)]
public static extern int Talk();
static void Main(string[] args)
{
int value=Talk();
}
}
}
非托管.h
#ifndef UNMANAGED_H
#define UNMANAGED_H
extern "C"
{
__declspec(dllexport) int Talk();
}
#endif
非托管.cpp
#include "stdafx.h"
#include "conio.h"
#include "Unmanaged.h"
int Talk()
{
int x=10,y=5;
return (x+y);
}
【问题讨论】:
-
您是否已将非托管 DLL 复制到与 C# 可执行文件相同的文件夹中? Edit 等等,你是在调用 EXE 吗?你不应该那样做。您需要创建一个 DLL。
-
转到您的客户端应用程序-->添加-->现有项目--> 找到生成 dll 的路径-->选择 DLL --> 添加为链接完成了