将字符数组值从C 传递到C#应用
Pass char array value from C++ to C# app
我有一个巨大的C 结构,其中我想在C#项目中导入大量验证代码。除了char*和char [],我能够转移所有值。
用char*,我的字符串充满了thiness torcater,但是,如果我在内存中看,我的字符串在那里,我可以看到"这是一个测试#1"。
使用char [x],我只能看到1rst char,在内存中相同。
在以下测试中,我可以提取整数:value1 = data.value1;和值1是123,但是char。
问题:我想念什么,为什么我无法用char阵列获得价值。谢谢你
C DLL
//This is the main DLL file.
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
extern "C"
{
public struct Data
{
int Value1;
char* pValue2;
char Value3[1024];
};
typedef int( *FN_CCP_INVOKE_NEW_BOARD_OPTIMIZER) (struct Data* data);
FN_CCP_INVOKE_NEW_BOARD_OPTIMIZER _pInvokeCallback;
int __declspec(dllexport) DLLTestCPlusPlus_Initialize()
{
return 0;
}
int __declspec(dllexport) DLLTestCPlusPlus_RegisterDllInvokeProcessCallback(void* fnInvokeCaller)
{
_pInvokeCallback = (FN_CCP_INVOKE_NEW_BOARD_OPTIMIZER)fnInvokeCaller;
struct Data data;
// INT
data.Value1 = 123;
// CHAR*
data.pValue2 = new char[1024];
sprintf(data.pValue2, "This is a test #1");
// CHAR [1024]
sprintf(data.Value3, "This is a test #2");
if (_pInvokeCallback)
{
_pInvokeCallback(&data);
}
return 0;
}
}
这是导入dll的C#程序。
using System;
using System.Runtime.InteropServices;
public unsafe struct Data
{
public int Value1;
public char* pValue2;
public fixed char Value3[1024];
}
public static class Interop
{
public delegate Int32 Callback([MarshalAs(UnmanagedType.Struct)] Data data);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDllDirectory(string lpPathName);
[DllImport("C:\DATA\CODE\ApplicationTestCSharp\x64\Debug\DLLTestCPlusPlus.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 DLLTestCPlusPlus_Initialize();
[DllImport("C:\DATA\CODE\ApplicationTestCSharp\x64\Debug\DLLTestCPlusPlus.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 DLLTestCPlusPlus_RegisterDllInvokeProcessCallback([MarshalAs(UnmanagedType.FunctionPtr)] Callback handler);
}
public class MyTest
{
private Interop.Callback _callback = null;
public MyTest()
{
int returnCode = 0;
returnCode = Interop.DLLTestCPlusPlus_Initialize();
_callback = new Interop.Callback(CallbackHandler);
returnCode = Interop.DLLTestCPlusPlus_RegisterDllInvokeProcessCallback(_callback);
}
private Int32 CallbackHandler(Data data)
{
int value1 = 0;
string value2 = "";
string value3 = "";
unsafe
{
// INT
value1 = data.Value1;
// CHAR* - MUST BE "This is a test #1"
value2 = new string(data.pValue2);
// CHAR [1024] - "This is a test #2"
value3 = new string(data.Value3);
}
return 1;
}
}
class Program
{
static void Main(string[] args)
{
MyTest myTest = new MyTest();
}
}
好吧,我找到了,我在结构声明中进行了更改
/*
public char* pValue2;
public fixed char Value3[1024];
*/
[MarshalAs(UnmanagedType.LPStr)] public String pValue2;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] public String pValue3;
并像这样拉数据!
// CHAR* - "This is a test #1";
// value2 = new string(data.pValue2);
value2 = data.pValue2
// CHAR [1024] - "This is a test #2"
//value3 = new string(data.Value3);
value3 = data.pValue3;
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- c ++控制台应用程序,如何在控制台外部打印字符
- 将字符数组值从C 传递到C#应用
- 将字符数组另存为 JPG C++用于 Windows 应用商店应用
- 为什么非unicode应用程序系统语言环境使Unicode字体具有错误显示的符号字符
- 为什么不能在常量字符 * str 上应用 str.find_last_of( )
- Unicode 字符变为问号(C++ 和基于 MFC 对话框的应用)
- Tizen IDE中的本机应用程序:签名崩溃并打印奇怪的非ASCII字符
- 对字符字符串数组应用 c++ "lower_bound"
- 在C++控制台应用程序中计算单词中的字符数
- 如何在c++控制台应用程序中输出波兰语字符
- 字符串比较返回值(Is用于对字符进行排序的应用程序?)
- 在c++中不应用字符-
- QT应用程序中的波兰字符
- 在MBCS操作系统上运行单字节字符应用程序的风险
- 使用c++,在制作控制台应用程序时,是否有一种方法可以让cin接受输入的下一个字符并在不按回车键的情况下继续