C# 调用Delphi 方法 指针参数处理

C#项目调用 Delphi 方法,其中

Delphi 声明:

function SendEx(AName,ACommand,AContent:PAnsiChar;var AResult:PAnsiChar):Boolean; stdcall;

测试  参数使用 string,stringBulider,IntPtr,string[] 都无果

============================重点开始==========================

IntPtr Test= Marshal.StringToHGlobalAnsi("Test");//错误

========》正确:Test=mallocIntptr("Test");

原因不晓得,有空在去了解;

============================重点结束==========================

C#:

 [DllImport(@"E:\Test.dll", EntryPoint = "SendEx", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  public static extern Boolean SendEx(IntPtr AName, IntPtr ACommand, IntPtr AContent, ref IntPtr AResult);

调用:

 

  private void IPC_IntPtr_Delphi_Click(object sender, EventArgs e)
        {
            try
            {
                IntPtr AServerName, ACommand, AContent, AResult;
                //SendEx("MyTestIPC", "Command", "测试", ref AResult);
                //根据数据的长度申请非托管空间   
                AName = mallocIntptr("MyTestIPC");
                ACommand = mallocIntptr("Command");
                AContent = mallocIntptr("测试---IntPtr--");
                AResult = mallocIntptr("测试---IntPtr--");
                bool res = SendEx(AName, ACommand, AContent, ref AResult);
                if (res) { string sss = Marshal.PtrToStringAnsi(AResult); }
                Marshal.FreeHGlobal(AName);
                Marshal.FreeHGlobal(ACommand);
                Marshal.FreeHGlobal(AContent);
                Marshal.FreeHGlobal(AResult);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        /// <summary>   
        /// 根据数据的长度申请非托管空间   
        /// </summary>   
        /// <param name="strData">要申请非托管空间的数据</param>   
        /// <returns>指向非拖管空间的指针</returns>   
        private static IntPtr mallocIntptr(string strData)
        {
            //先将字符串转化成字节方式   
            Byte[] btData = System.Text.Encoding.Default.GetBytes(strData);

            //申请非拖管空间   
            IntPtr m_ptr = Marshal.AllocHGlobal(btData.Length);

            //给非拖管空间清0    
            Byte[] btZero = new Byte[btData.Length + 1]; //一定要加1,否则后面是乱码,原因未找到   
            Marshal.Copy(btZero, 0, m_ptr, btZero.Length);

            //给指针指向的空间赋值   
            Marshal.Copy(btData, 0, m_ptr, btData.Length);

            return m_ptr;
        }

   /// <summary>   
        /// 根据长度申请非托管空间   
        /// </summary>   
        /// <param name="strData">要申请非托管空间的大小</param>   
        /// <returns>指向非拖管空间的指针</returns>  

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#调用C++的方法,可以使用平台调用(Platform Invocation Services,P/Invoke)来实现。下面是一个简单的例子,演示了如何在C#调用一个C++函数,该函数接受一个结构体指针作为参数。 首先,我们需要在C#中定义一个结构体,这个结构体的成员和C++中的结构体成员应该是一一对应的。例如: ``` [StructLayout(LayoutKind.Sequential)] public struct MyStruct { public int field1; public float field2; } ``` 然后,我们需要在C#中声明C++函数的签名,这个签名应该包括函数名、返回值类型参数列表。例如: ``` [DllImport("mylib.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void MyFunction(ref MyStruct myStruct); ``` 其中,`mylib.dll`表示C++的动态链接库文件名,`CallingConvention.Cdecl`表示调用规约,`void`表示返回值类型,`ref MyStruct myStruct`表示一个引用参数,它将传递一个指向MyStruct结构体的指针。 最后,我们可以在C#调用C++函数,例如: ``` MyStruct myStruct = new MyStruct(); myStruct.field1 = 123; myStruct.field2 = 3.14f; MyFunction(ref myStruct); ``` 这将会调用C++函数,并将myStruct结构体的指针传递给它。在C++函数中,我们可以使用指针来访问结构体的成员。例如: ``` void MyFunction(MyStruct* pMyStruct) { int field1 = pMyStruct->field1; float field2 = pMyStruct->field2; // do something with the fields... } ``` 注意,在C++中定义结构体时,需要使用`__declspec(dllexport)`修饰符来导出结构体定义,以便在C#中使用。例如: ``` #pragma once #ifdef MYLIB_EXPORTS #define MYLIB_API __declspec(dllexport) #else #define MYLIB_API __declspec(dllimport) #endif struct MYLIB_API MyStruct { int field1; float field2; }; ``` 其中,`MYLIB_EXPORTS`是一个宏,用于指示我们正在编译动态链接库而不是使用它。在C++中,我们可以使用`#ifdef`指令根据这个宏来定义不同的行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值