次のように、管理されていない非常に単純な C++ 関数 ( にあります) を C# で管理されている関数から呼び出してJNIDiskInfoDll.dll
います。
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;
}
}
}
}
私の問題は、アプリケーションを実行した後、 と という 2 つの連続したエラーが発生することprojectName.exe has triggered a breakpoint
ですUnhandled exception at 0x77110E23 (ntdll.dll) in projectName.exe: 0xC0000374: A heap has been corrupted (parameters: 0x7712E930).
。これらのエラーが発生していますが、関数はまだ目的の出力を返していることを知っています。
エラーが続くコード全体を削除する前にC関数を使用していたため、 getSerial
C関数にはパラメーターがあることに注意してください(のみ保持)。LPTSTR inCStrIn
return "abcdefg";
ここで何が問題になるのかわかりません。を に変更しようとしCharset
ましたDllImport
がUnidcode
、それでも同じエラーが発生します。何か助けてください。