从C#传递字符串数组C编程 [英] Passing string array to c programming from C#

查看:361
本文介绍了从C#传递字符串数组C编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用非托管C ++编程code,从C#.NET托管code。但我无法通过字符串数组作为参数从.net的功能
在C功能:

I am calling unmanaged C programming code from C# .Net managed code. But I am unable to pass string array as a parameter to function from .Net Function in C:

dllmain(const int argc, const char *argv[]){}

请帮帮我,我该怎么从C Sharp.Net调用这个函数。

Please help me, how I can call this function from C Sharp.Net

感谢。

推荐答案

下面是一种方法,你可以这样做:

Here is a way you can do that:

C#code:


  1. 声明C函数,你想从C#调用

  1. Declare C function you want call from C#

[DllImport("<DLL_File_Name>.dll", CharSet = CharSet.Auto, EntryPoint = "dllmain", ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
public static extern void dllmain(ref string str1);


  • 定义字符串

  • Define the string

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
        public string str1;
    


  • 从调用C#code中的C函数

  • Call the C function from C# code

        dllmain(ref str1);
    


  • C code:


    1. 函数原型

    1. Function prototype

    __declspec(dllexport) void __stdcall dllmain(char *str1);
    


  • 函数定义

  • Function definition

    void__stdcall dllmain(char *str1)
    {
        :
    }
    


  • 希望这将是对你有帮助。 : - )

    Hope this will be helpful for you. :-)

    这篇关于从C#传递字符串数组C编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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