Karthik V

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.



Re: Visual C# General How do I pinvoke this function?

Figo Fei - MSFT

Hi, Karthik

What did you attempt to do with SomeFunc

It seems that you've already post the same question in the newsgroup, for others reference: http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/91791d2352f2008c/3068904e2f05dba1 lnk=st&q=How+do+I+pinvoke+this+function&rnum=1&hl=zh-CN#3068904e2f05dba1

Thanks




Re: Visual C# General How do I pinvoke this function?

Karthik V

Its part of a DLL for which I don't have the source. That function sends an system image to a server. When the image is being transferred, the progress of the transfer is notified by calling the UpdateProgress method on the passed object.



Re: Visual C# General How do I pinvoke this function?

Figo Fei - MSFT

Hi, Karthik

You can not directly pass a C++ class to C#, we got no ideas about the underlying memory layout of the native C++ class.

I agree with the Willy (MVP in the newsgroup) replied:

"It looks like IProgress is a native C++ class, you can't use such classes
directly from C#, you will have to wrap this class in a managed wrapper
using C++/CLI (VS2005 C++ using /clr option)."

Thanks






Re: Visual C# General How do I pinvoke this function?

Karthik V

I understand. Thanks a lot. I was going to write a wrapper but wanted to confirm that there is no workaround.