Created
February 3, 2020 18:35
-
-
Save kimmoli/d8a46c5e95caa70a909760851fdcbbfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* C++ */ | |
typedef struct { | |
unsigned long address; | |
unsigned long length; | |
unsigned char * data; | |
} ELEMENT, *PELEMENT; | |
unsigned long SetElement(ELEMENT Element); | |
/* C# */ | |
[StructLayout(LayoutKind.Sequential, Pack = 1)] | |
public unsafe struct Element | |
{ | |
public unsigned long address; | |
public unsigned long length; | |
public IntPtr data; | |
} | |
[DllImport("MyDll.DLL", EntryPoint = "SetElement", CharSet = CharSet.Auto)] | |
public static unsafe extern unsigned long SetElement(Element element); | |
FileInfo fi = new FileInfo(filename); | |
Element element = new Element | |
{ | |
address = startAddress, | |
length = (unsigned int)fi.Length, | |
}; | |
element.data = Marshal.AllocHGlobal((unsigned int)fi.Length); | |
byte[] file = File.ReadAllBytes(filename); | |
Marshal.Copy(file, 0, element.data, (unsigned int)fi.Length); | |
SetElement(element); | |
Marshal.FreeHGlobal(element.data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment