将字符串作为PChar从CSharp传递到Delphi DLL [英] Passing string as PChar from CSharp to Delphi DLL

查看:165
本文介绍了将字符串作为PChar从CSharp传递到Delphi DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将C#中的字符串传递给Delphi构建的DLL。
Delphi DLL期望PChar。

I'm trying to pass a string from C# to Delphi built DLL. Delphi DLL expects PChar.

这里是Delphi导出

Here is Delphi export

procedure DLL_Message(Location:PChar;AIntValue :integer);stdcall;
external 'DLLTest.dll';

C#import(我尝试的最后一个,有字符串,char * ref字符串...)

C# import(last one I tried, there were string, char* ref string ...)

[DllImport(
            "DLLTest.dll", 
            CallingConvention = CallingConvention.StdCall,
            CharSet = CharSet.Ansi,
            EntryPoint = "DLL_Message"
        )]
        public static extern void DLL_Message(IntPtr Location, int AIntValue);

我以任何方式获得访问冲突访问值。

I get access violation accessing value in any way.

在C#中是否有任何解决方案将字符串值作为PChar传递?

Is there any solution to pass a string value as PChar in C#?

推荐答案

尝试使用MarshalAs属性,该属性允许您可以控制使用的本机类型。

Try with the MarshalAs attribute that allows you to control the native type that is used.

可以在MSDN中找到类型列表:

A list of types can be found in MSDN:

http://msdn.microsoft.com/de-de /library/system.runtime.interopservices.unmanagedtype.aspx

DllImport(
            "DLLTest.dll", 
            CallingConvention = CallingConvention.StdCall,
            CharSet = CharSet.Ansi,
            EntryPoint = "DLL_Message"
        )]
        public static extern void DLL_Message(
            [MarshalAs(UnmanagedType.LPStr)] string Location,
            int AIntValue
        );

这篇关于将字符串作为PChar从CSharp传递到Delphi DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆