473,413 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

How do I pinvoke this function?

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.

Jun 21 '07 #1
6 2469
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]
- mv*@spam.guard.caspershouse.com
"Karthik V" <ka**************@gmail.comwrote in message
news:11**********************@g37g2000prf.googlegr oups.com...
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.

Jun 21 '07 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2******************@TK2MSFTNGP04.phx.gbl...
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
It looks like a COM interface pointer to me... or at least binary
compatible. No IUnknown or IDispatch support though.

You'll need a type library for it, and to run tlbimp.

The lack of IUnknown may cause problems.
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]
- mv*@spam.guard.caspershouse.com
"Karthik V" <ka**************@gmail.comwrote in message
news:11**********************@g37g2000prf.googlegr oups.com...
>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.


Jun 21 '07 #3
That's the thing, if it doesn't derive from IUnknown, then it can't
possibly be a COM interface pointer. Just because it is an interface
doesn't mean that it is supported by COM.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2******************@TK2MSFTNGP04.phx.gbl...
>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

It looks like a COM interface pointer to me... or at least binary
compatible. No IUnknown or IDispatch support though.

You'll need a type library for it, and to run tlbimp.

The lack of IUnknown may cause problems.
>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]
- mv*@spam.guard.caspershouse.com
"Karthik V" <ka**************@gmail.comwrote in message
news:11**********************@g37g2000prf.googleg roups.com...
>>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.



Jun 21 '07 #4
"Karthik V" <ka**************@gmail.comwrote in message
news:11**********************@g37g2000prf.googlegr oups.com...
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.

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).

Willy.

Jun 21 '07 #5

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:eP**************@TK2MSFTNGP05.phx.gbl...
That's the thing, if it doesn't derive from IUnknown, then it can't
possibly be a COM interface pointer. Just because it is an interface
doesn't mean that it is supported by COM.
COM is as much a binary standard for v-table layout as anything else, and
that interface would meet the COM binary standard.
>
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2******************@TK2MSFTNGP04.phx.gbl...
>>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

It looks like a COM interface pointer to me... or at least binary
compatible. No IUnknown or IDispatch support though.

You'll need a type library for it, and to run tlbimp.

The lack of IUnknown may cause problems.
>>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]
- mv*@spam.guard.caspershouse.com
"Karthik V" <ka**************@gmail.comwrote in message
news:11**********************@g37g2000prf.google groups.com...
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.



Jun 21 '07 #6
Ben,

While that is the case, COM demands that all interfaces derive from
IUnknown. If an interface has to derive from that, then the vtable for that
interface is always going to be populated with the entries for the methods
in IUnknown in the first three slots, follwed by the methods for the
interface itself. Since the interface here doesn't derive from IUnknown,
its vtable is not going to have those entries.

There is no way that this interface could be used in COM.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:eP**************@TK2MSFTNGP05.phx.gbl...
> That's the thing, if it doesn't derive from IUnknown, then it can't
possibly be a COM interface pointer. Just because it is an interface
doesn't mean that it is supported by COM.

COM is as much a binary standard for v-table layout as anything else, and
that interface would meet the COM binary standard.
>>
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>>
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2******************@TK2MSFTNGP04.phx.gbl...
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

It looks like a COM interface pointer to me... or at least binary
compatible. No IUnknown or IDispatch support though.

You'll need a type library for it, and to run tlbimp.

The lack of IUnknown may cause problems.

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]
- mv*@spam.guard.caspershouse.com
"Karthik V" <ka**************@gmail.comwrote in message
news:11**********************@g37g2000prf.googl egroups.com...
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.
>




Jun 21 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to execute a function of a unmanaged dll using PInvoke, i have definied the function as: public static extern int isc_dsql_prepare( int status_vector, ref int...
3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
1
by: cppdev | last post by:
Hello, After reading a few articles, http://blogs.gotdotnet.com/cbrumme/PermaLink.aspx/e55664b4-6471-48b9-b360-f0fa27ab6cc0...
4
by: yaron | last post by:
Hi, I want to use my unmanaged c++ class library from a c# client. my unmanaged c++ class library use polymorpism, is this polymorphism also exported to my c# client via PInvoke ? thanks.
5
by: _iycrd | last post by:
After numerous problems, I'm having second thoughts about using C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like it will take a huge amount of work, if it will work at all. ...
3
by: msnews.microsoft.com | last post by:
Hi i am using User32.dll in Visual stdio 2005. public static extern long SetActiveWindow(long hwnd); public static extern long keybd_event(byte bVk, byte bScan, long dwFlags,
6
by: Pucca | last post by:
I have a program that originally compiles into a exe file. I changed the compile option to generate dll file. This program calls a com component. Can I use pinvoke in C# to call it? The...
8
by: Rajesh Soni | last post by:
Hi! I'm getting a PInvoke error while trying to execute the following code... declaration: Structure POINTAPI Dim x As IntPtr
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.