Re: How do I pinvoke this function?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Karthik,

I believe in this case that IProgress is not actually a class, but an
interface pointer. The thing is, while you will be able to call it in C#,
you won't be able to call the methods on the interface pointer, as it needs
to be a COM interface pointer in order to interop correctly. .NET interop
does not support native C++ interfaces.

Your P/Invoke declaration would look like this:

[DllImport("THE_DLL.dll", SetLastError = true)]
static extern void SomeFunc(IntPtr handle, IntPtr progress);

And the best you could do with the progress pointer is pass it around.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx


"Karthik V" <karthikveeramani@xxxxxxxxx> wrote in message
news:1182443257.074652.317580@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
In a certain dll, there is this function:
typedef DWORD STDAPICALLTYPE SOMEFUNC(VOID *pHandle, IProgress
*pProgress);

where IProgress is
class IProgress { public: virtual void Update(UINT progress) = 0; };

To pinvoke this from C#, I tried the following:
[DllImport("THE_DLL.dll", SetLastError = true)]
static extern void SomeFunc(IntPtr handle, Progress progress);

and defined Progress as
public class Progress
{
public void Update(ushort progress)
{
// some code
}
}

Trying to invoke SomeFunc and passing an object of Progress, I get an
"Attempted to read or write protected memory" error. The other methods
in the same dll that don't involve the progress callback object (but
involve the handle parameter) work fine.

Please let me know how to pinvoke this function.



.



Relevant Pages

  • Re: How do I pinvoke this function?
    ... you won't be able to call the methods on the interface pointer, ... And the best you could do with the progress pointer is pass it around. ... IProgress ... static extern void SomeFunc ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I pinvoke this function?
    ... possibly be a COM interface pointer. ... No IUnknown or IDispatch support though. ... And the best you could do with the progress pointer is pass it around. ... static extern void SomeFunc ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I pinvoke this function?
    ... possibly be a COM interface pointer. ... that interface would meet the COM binary standard. ... No IUnknown or IDispatch support though. ... And the best you could do with the progress pointer is pass it ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I pinvoke this function?
    ... IProgress ... Progress progress); ... in the same dll that don't involve the progress callback object (but ... Please let me know how to pinvoke this function. ...
    (microsoft.public.dotnet.languages.csharp)
  • How do I pinvoke this function?
    ... In a certain dll, there is this function: ... IProgress ... Progress progress); ... Please let me know how to pinvoke this function. ...
    (microsoft.public.dotnet.languages.csharp)