- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我想到的:
非托管函数:
extern "C" __declspec(dllexport) char* callme(const char * sing) {
char buf[10];
sprintf(buf,"hey %s",sing);
return buf;
}
调用函数:
class Program
{
[DllImport("testnonclr.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.Cdecl)]
public static extern IntPtr callme([In,MarshalAs(UnmanagedType.LPStr)]
string sing);
static void Main(string[] args) {
Console.Write( Marshal.PtrToStringAuto (callme("baby") ));
Console.ReadLine();
}
}
结果:一堆乱码
好的,感谢一些用户的帮助和更多的挖掘,这就是我最终所做的:
extern "C" {
__declspec(dllexport) char* __stdcall callme(const char * sing)
{
static char *buf=NULL;
char fubb[10];
sprintf(fubb,"hey %s",sing);
ULONG usize=strlen(fubb)+sizeof(char);
buf=(char *)::GlobalAlloc (GMEM_FIXED,usize);
strcpy(buf,fubb);
return buf;
}
}
和 C#:
class Program
{
[DllImport("testnonclr.dll", CharSet = CharSet.Unicode, CallingConvention = C
CallingConvention.StdCall )]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string callme([In, MarshalAs(UnmanagedType.LPStr)] string sing);
static void Main(string[] args)
{
string rrr = callme("baby");
Console.WriteLine(rrr);
Console.ReadLine();
}
}
最佳答案
您不能返回指向局部变量的指针,因为这些变量是在堆栈上分配的(并且当函数返回时堆栈可能会被销毁)。
您需要在 C 函数的堆上分配内存(最好使用 CoTaskMemAlloc
,以便 CLR 稍后可以释放内存)并返回指向该内存的指针。
顺便说明一下,编码时可以直接返回 string
(假设它是一个以 null 结尾的 c Unicode 字符串),无需返回 IntPtr
。
更多信息请点击:http://limbioliong.wordpress.com/2011/06/16/returning-strings-from-a-c-api/
关于返回 char* 的 C 函数的 C# pinvoke,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27463765/
这是我尝试在 C# 中使用的头文件签名和 C 代码: __declspec(dllexport) emxArray_real_T *emxCreateWrapper_real_T(real_T *da
我的项目在 .NET Frame work 3.5 中运行成功且没有错误。但是,当我将它定位到 .NET Frame work 4 时。我得到了错误: “对 PInvoke 函数的调用使堆栈不平衡。这
我在非托管 DLL 中有一个非常简单的函数,但我没有从它获得正确的返回值。 我可以确认通用 PInvoke 机制正在使用我的 C DLL 中的一个函数: /* Return an integer */
使用 PInvoke 时可能会发生哪些异常,或者所有错误均由方法返回值处理,并且由开发人员检查并在需要时引发异常? 最佳答案 对于 P/Invoke,可以肯定地说您需要处理两种类型的错误。 P/Inv
我正在尝试为 modbusm.dll (win-tech.com/html/mbusocx.htm) 创建一个包装器 C# 文件,为此我使用 dumpbin 输出。 文件 modbusm.dll 的转
我正在尝试使用 PInvoke 绑定(bind)此 C 函数。 bool GuiTextBox(Rectangle bounds, char *text, int textSize, bool edi
我目前正在尝试通过 C# pinvoke 使用 SendMessage 从子窗口获取一些文本。但是,我之前对窗口句柄进行硬核化的尝试失败了,因为该值在应用程序启动时发生了变化。有没有办法可靠地获取这个
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
考虑以下 C 函数: void get_lib_version(const char **ver_string); 如何使用 PInvoke 正确编码它?文档说它返回一个指向静态字符串的指针。我认为这
我有个问题,很奇怪。当我在 PInvoke 中使用下一个结构时,它正在工作,但是当我将它更改为类时,它不起作用。 我的 C#: [StructLayout(LayoutKind.Sequential)
我在 C++ 原生 dll 中有以下函数,我想在 C# 应用程序中使用它。 DWORD __cdecl Foo( LPCTSTR Input,
似乎有多种方法可以做到这一点,但我尝试过的示例对我不起作用。 我在某处读到,使用不安全指针是获得需要指针的更复杂结构的方法。 (对不起,我没能再次找到来源) 我最近的尝试看起来像这样: unsafe
我正在尝试在 C++ 中创建一个简单的 dll,并使用 PInvoke 从 C# 中调用它。我想传入一个字节数组,对其进行一些操作,然后发回另一个字节数组。我想我会传入框架和大小,然后创建一个非托管的
我正在尝试使用 P/Invoke 从 C# 调用 C++ 函数。 [DllImport(PATH)] public static extern int PQunescapeByteaWrapper(
我正在尝试使用 GetFileInformationByHandle 函数获取 FILE_ID_BOTH_DIR_INFO。调用后我的所有值都设置为零。 我收到 win32 错误消息: ERROR_B
我有以下 C 代码: const BYTE* Items[3]; Items[0] = item1; Items[1] = item2; Items[2] = item3; int result =
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我有以下 PInvoke:(C 到 C#) [DllImport("chess_api.dll", CallingConvention = CallingConvention.Cdecl)] stat
我正在尝试在 C# 项目中使用 C DLL。 我在 C 中有一个函数: extern __declspec(dllexport) void InitBoard(sPiece board[8][8]);
我如何在 C# 中使用这个 dll 函数?我尝试了以下但出现错误。“外部组件抛出异常。” 我第一次用 C# 和 Delphi 做这个 PInvoke 的东西。 function HTTPGET(loc
我是一名优秀的程序员,十分优秀!