I am calling an un-managed and very simple C++ function (located in JNIDiskInfoDll.dll
) from a C# managed one as follows:
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;
}
}
}
}
My problem is that after running the application I get two successive errors saying projectName.exe has triggered a breakpoint
and Unhandled exception at 0x77110E23 (ntdll.dll) in projectName.exe: 0xC0000374: A heap has been corrupted (parameters: 0x7712E930).
. Knowing that although I'm getting these errors, the function is still returning the desired output.
Please note that the getSerial
C function has the LPTSTR inCStrIn
parameter since I was using it before I did remove the whole code (keeping only return "abcdefg";
) where the error persists.
I don't know what could be the problem here. I tried to change the Charset
in the DllImport
into Unidcode
, but still getting same errors. Any help please?