Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: KatzYaakov on December 27, 2019, 08:21:35 AM

Title: cant write to registry by Api C#
Post 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>



Title: Re: cant write to registry by Api C#
Post by: smurph on December 27, 2019, 03:16:17 PM
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
Title: Re: cant write to registry by Api C#
Post by: KatzYaakov on December 28, 2019, 11:15:57 AM
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);

        }
Title: Re: cant write to registry by Api C#
Post by: smurph on December 28, 2019, 06:48:54 PM
Still your function signature is wrong for returning a value in the third parameter.  It is a passed pointer.

Code: [Select]
[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
Title: Re: cant write to registry by Api C#
Post by: KatzYaakov on December 29, 2019, 01:32:21 AM
thanks a lot ,it work perfect