Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
i'm building an application in visual studio c# to work with a device.
at some point i have my device handle in an intptr variable.

then i have another process that needs to use the same handle.
the way i passed this information was by doing this in the primary app:

IntPtr h = ;//"the function that returns the device handle"
ProcessStartInfo p = new ProcessStartInfo("otherapp.exe", h.ToString());
Process.Start(p);

and in the other app i catch the string parameter and then do this:

IntPtr h = new IntPtr(Int32.Parse(args[0]));

so far everything works but when i try to use the new handle in the second app it throws an error:
trying to access protected memory

can someone help me please????

What I have tried:

the way i passed this information was by doing this in the primary app:

IntPtr h = ;//"the function that returns the device handle"
ProcessStartInfo p = new ProcessStartInfo("otherapp.exe", h.ToString());
Process.Start(p);

and in the other app i catch the string parameter and then do this:

IntPtr h = new IntPtr(Int32.Parse(args[0]));

so far everything works but when i try to use the new handle in the second app it throws an error:
trying to access protected memory
Posted
Updated 30-Jan-17 9:19am
Comments
Richard MacCutchan 30-Jan-17 12:45pm    
If it was possible to do what you are trying, then every PC in the universe would be destroyed in no time.
You need to create a shared memory object which both processes can access.

Not very elegant, but you could use the unsafe keyword.
Here is an explanation: How to: Use Pointers to Copy an Array of Bytes (C# Programming Guide)[^]
 
Share this answer
 
Comments
#realJSOP 30-Jan-17 14:40pm    
I don't think this is gonna help him. Modern versions of Windows sets up barriers between processes to specifically prohibit that kind of access
I think you're going about this the wrong way. Instead of having two different applications trying to access the same handle, which is prevented by security by the way, you only need one.

Create an application that handles all access to the device in question and performs all critical functions. Then you don't pass a handle around to other applications. You other applications cal tell this application what to do with the device/handle it has. interprocess communication (like named pipes) is quite a bit easier than screwing around with security trying to get this to work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900