Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: KatzYaakov on December 27, 2019, 08:21:35 AM
-
i use the Api to control the mach by c# and its work ok
now want to improve and also write into the registry by the api
but i got error in c# while try to get the registry handle
the c# code:
[DllImport(@"C:\Mach4Hobby\Mach4IPC.dll", EntryPoint = "mcRegGetHandle")]
public static extern int mcRegGetHandle(int mlnst, string regname);
private void manualloading(object sender, RoutedEventArgs e)
{
int debug= mcRegGetHandle(0, "iRegs0/auto");
}
attach is the error from vs
System.AccessViolationException
HResult=0x80004003
Message=An attempt was made to read or write to protected memory. This condition usually indicates that another memory is defective.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
-
The function signature is incorrect. Where it your variable to receive the register handle?
Here is the C definition.
MACHAPI int MACH_APIENTRY mcRegGetHandle(MINSTANCE mInst, const char *path, HMCREG *hReg);
Steve
-
i try wit 3 argument ,got same error
[DllImport(@"C:\Mach4Hobby\Mach4IPC.dll", EntryPoint = "mcRegGetHandle")]
public static extern int mcRegGetHandle(int mlnst, string regname,int hReg);
private void manualloading(object sender, RoutedEventArgs e)
{
int hreg = 0;
int debug= mcRegGetHandle(0, "gRegs0/auto",hreg);
}
-
Still your function signature is wrong for returning a value in the third parameter. It is a passed pointer.
[DllImport("Mach4Core.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int mcRegGetHandle(
[param: MarshalAs(UnmanagedType.I4)] int mInst,
[param: MarshalAs(UnmanagedType.LPStr)] string path,
[param: MarshalAs(UnmanagedType.U4), In(), Out()] ref uint hReg);
Steve
-
thanks a lot ,it work perfect