我正在JNIDiskInfoDll.dll
从 C# 托管函数调用一个非托管且非常简单的 C++ 函数(位于 中),如下所示:
C++:
#include "stdafx.h"
#include "AtaSmart.h"
#include <iostream>
#include <string.h>
extern "C" __declspec(dllexport) char* __cdecl getSerial(LPTSTR inCStrIn)
{
return "abcdefg";
}
C#:
using System;
using System.Runtime.InteropServices;
namespace HardInfoRetriever
{
class DiskInfoRetreiver
{
[DllImport("C:\\Users\\User1\\Documents\\Visual Studio 2017\\Projects\\HardInfoRetriever\\Debug\\JNIDiskInfoDll.dll",
EntryPoint = "getSerial", CallingConvention = CallingConvention.Cdecl,
BestFitMapping = false, ThrowOnUnmappableChar = true, CharSet = CharSet.Ansi)]
public static extern String getSerial([MarshalAs(UnmanagedType.LPTStr)]String _driveletter_);
public static String getSerialNumber(String driveletter)
{
try
{
return getSerial(driveletter);
}
catch (Exception e)
{
throw e;
}
}
}
}
我的问题是,在运行应用程序后,我收到两个连续的错误,说projectName.exe has triggered a breakpoint
and Unhandled exception at 0x77110E23 (ntdll.dll) in projectName.exe: 0xC0000374: A heap has been corrupted (parameters: 0x7712E930).
。知道虽然我遇到了这些错误,但该函数仍在返回所需的输出。
请注意,getSerial
C 函数具有LPTSTR inCStrIn
参数,因为我在删除return "abcdefg";
错误仍然存在的整个代码(仅保留)之前使用它。
我不知道这里可能是什么问题。我试图将Charset
in更改DllImport
为Unidcode
,但仍然遇到相同的错误。请问有什么帮助吗?