Hello Guest it is March 28, 2024, 06:47:24 PM

Author Topic: cant write to registry by Api C#  (Read 1022 times)

0 Members and 1 Guest are viewing this topic.

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



Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: cant write to registry by Api C#
« Reply #1 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
Re: cant write to registry by Api C#
« Reply #2 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);

        }

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: cant write to registry by Api C#
« Reply #3 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
Re: cant write to registry by Api C#
« Reply #4 on: December 29, 2019, 01:32:21 AM »
thanks a lot ,it work perfect