我是靠谱客的博主 优雅春天,最近开发中收集的这篇文章主要介绍c#调用c语言dll,形参有char*,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

c#调用c语言dll,形参有char*,无法获得char*内容。但是在c++中调用dll无问题。说明dll正确,C#中代码有问题。

直接解决方法:未解决,dll没问题,就是调用后存储在webtoken中读取有问题,求大神告知解决方法:

变相解决方法:修改dll文件,将形参char*作为返回值char*返回。

原先的c函数声明:

#ifndef _DLLMAIN_H  
#define _DLLMAIN_H  
#ifndef        DLL_EXPORT  
#define _LIBAPI __declspec(dllexport)  
#else  
#define _LIBAPI __declspec(dllimport)  
#endif  
#ifdef __cplusplus
extern "C" {
#endif
_LIBAPI void getWebToken(const char* appkey, const char* counter, char* webtoken)
#ifdef __cplusplus
}
#endif
#endif /* _DLL_H_ */

原先的调用方式:无法获得webtoken中的字符串。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        [DllImport("omp_sec_dll.dll", EntryPoint = "getWebToken", CallingConvention = CallingConvention.Cdecl)]

        public static extern void getWebToken1(string appkey, string counter, ref IntPtr webtoken);  

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string appkey = "123456781111";
            string counter = "876543211111";
            string txt = "testttttttttttt";
            IntPtr webtoken = Marshal.StringToHGlobalAnsi(txt);
            string temp = Marshal.PtrToStringAnsi(webtoken);
            getWebToken1(appkey, counter, ref webtoken);
            string result = Marshal.PtrToStringAnsi(webtoken);
            MessageBox.Show(result);
        }
    }
}


修改后的C函数声明:

#ifndef _DLLMAIN_H  
#define _DLLMAIN_H  
#ifndef        DLL_EXPORT  
#define _LIBAPI __declspec(dllexport)  
#else  
#define _LIBAPI __declspec(dllimport)  
#endif  
#ifdef __cplusplus
extern "C" {
#endif
_LIBAPI char* getWebToken(char* appkey, const char* counter);
#ifdef __cplusplus
}
#endif
#endif /* _DLL_H_ */

变相解决后的调用方式:修改dll函数返回值为char*,修改appkey为IntPtr,此时能正常获得char*。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;//add

namespace TestGetWebToken
{
    public partial class Form1 : Form
    {
        [DllImport("omp_sec_dll.dll", EntryPoint = "getWebToken", CallingConvention = CallingConvention.Cdecl)] //add

        public static extern IntPtr getWebToken1(IntPtr appkey, string counter);  //add

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr appkey = Marshal.StringToHGlobalAuto("123456781111");
            string counter = "876543211111";
            IntPtr tmp = Java_com_cmcc_omp_WebSecuritySubassembly_getWebToken1(appkey, counter);
            string result = Marshal.PtrToStringAnsi(tmp);
            MessageBox.Show(result);
        }
    }
}

总结:

1. const char* 直接换成string

2. char*做形参或返回值,需要换成IntPtr

3. char*做形参并想要获取char*内容,使用ref IntPtr无用。只能将该char*改为返回值获得。


最后

以上就是优雅春天为你收集整理的c#调用c语言dll,形参有char*的全部内容,希望文章能够帮你解决c#调用c语言dll,形参有char*所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部