我是靠谱客的博主 优雅春天,这篇文章主要介绍c#调用c语言dll,形参有char*,现在分享给大家,希望可以做个参考。

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

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

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

原先的c函数声明:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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中的字符串。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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函数声明:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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*。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部