0

I am writing a dll which receives a picture box'handle in a call like:

**Declare Sub ERPInitialize Lib "C:\Desenv\EBioNet\EBioNet\Debug\ERPDll.dll" (ByVal PicHandle As Long)**

and it is called in this way:

**Call ERPInitialize(PictureBox1.Handle)**

I would like to store this handle and get back the PictureBox1's managed pointer back from the stored handle so that I can draw an image on it.

Any clue? Thanks in advance

If I pass PictureBox^ in the the call ERPInitialize I cannot stored it, for later use as a global, because managed pointers are not allowed in CLR.

I tried

PictureBox ^_pbPic;
IntPtr Ptr_Picturebox = (IntPtr)PictureHWD;
GCHandle handle2 = (GCHandle)Ptr_Picturebox;
_pbPic = (PictureBox ^ )(handle2.Target);

but it causes an exception 0xc0000005.

user3358125
  • 123
  • 10
  • Possible duplicate of [C# - How To Convert Object To IntPtr And Back?](http://stackoverflow.com/questions/17339928/c-sharp-how-to-convert-object-to-intptr-and-back) – Baldrick Aug 24 '16 at 14:13
  • It is *very* unlikely that using the Declare keyword is correct. Since you are writing C++/CLI code anyway, you might as well add a reference to the project so you can use managed declarations and pass a PictureBox^ reference directly. Nevertheless, the oracle you asks for [does exist](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.fromhandle%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396). Don't use it. – Hans Passant Aug 24 '16 at 14:16
  • Hans, What do you mean by the Declare (statement) is not correct? Can you please explain? – user3358125 Aug 24 '16 at 15:51
  • You are making an unmanaged call into managed code. Not only is it unnecessary and makes you ask this question, it also makes exceptions hard to diagnose. – Hans Passant Aug 24 '16 at 17:07
  • If I new this approach was unnecessary I wouldn't ask – user3358125 Aug 25 '16 at 02:34

0 Answers0