I have this code in c++ which I exported by a dll:
typedef struct {
unsigned short major;
unsigned short minor;
} Version;
EXPORTED_FUNC Result Init(Version *version, char *file);
extern "C" Result Init(Version *version, char *file)
{
if (file) {
if (!GFile.init(string(file))) {
return INVALID_PARAMETER;
}
if (version) {
version->major = VERSION_MAJOR1;
version->minor = VERSION_MAJOR2;
}
return OK;
}
I'm calling the dll from c#, and this is what I wrote there:
internal struct Version
{
ushort major { set; get; }
ushort minor { set; get; }
}
[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
static extern Result Init(ref Version versionInfo, [MarshalAs`(UnmanagedType.LPStr)] string FilePath);
and this is the call for Init:
string filePath = Application.StartupPath + "\\ABC.ini";
Version version = new Version();
result = _mydllWrapper.Init(ref version, filePath);
for the all the above code when I'm running the c# application I sometimes get in x64 machines the following exception:
Unable to load DLL mydll.dll : invalid access to memory location (Exception from HRESULT.0x800703E6)
How can I fix this code WITHOUT removing any security flags from compilation? code sample for the fix is really wellcome!
thanks!