【发布时间】:2015-09-14 19:49:56
【问题描述】:
我在 Win32 DLL 中的 .c 文件中调用 doThis 函数。
#include <stdio.h>
__declspec(dllexport) double doThis( char *message)
{
printf("do nothing much");
return 32.5;
}
使用此调用代码:
[DllImport(@"\\vmware-host\Shared Folders\c-sharp\Hot\MusicIO\Debug\HelloWorld.dll",
CallingConvention=CallingConvention.Cdecl)]
public static extern double doThis(string message);
private void button1_Click(object sender, EventArgs e)
{
double returned = doThis("what 2");
MessageBox.Show("Click " + returned);
}
这很好用,但我希望函数返回 char *... 并返回 message 变量。
当我将doThis 更改为返回char *,并且调用代码期望string 时,Win32 主机在运行时崩溃。
有什么建议吗?
[奇怪的是,我想我之前有这个工作]
【问题讨论】:
-
所以我将声明更改为
__declspec(dllexport) char *doThis( char *message),然后在调用方返回message...,`public static extern string doThis(string message);`...跨度> -
谢谢@4566976,很有用,但我很高兴得到下面接受的答案。