473,320 Members | 1,910 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,320 software developers and data experts.

WinAPI that returns a funtion ptr

Hello,

I was translating some sample from MSDN from C to VB .NET, and got the
following:

~
Private Declare Function GetProcAddress Lib "kernel32.dll" (ByVal hModule As
IntPtr, <MarshalAs(UnmanagedType.LPStr)> ByVal lpProcName As String) As
<MarshalAs(UnmanagedType.FunctionPtr)> DLLGETVERSIONPROC

Private Delegate Function DLLGETVERSIONPROC(ByRef dvi As DLLVERSIONINFO) As
Integer
Private Function GetDllVersion(ByVal lpszDllName As String) As Integer

Dim hinstDll As IntPtr

Dim dwVersion As Integer = 0

hinstDll = LoadLibrary(lpszDllName)

If hinstDll.ToInt32 <> 0 Then

Dim pDllGetVersion As DLLGETVERSIONPROC

pDllGetVersion = GetProcAddress(hinstDll, "DllGetVersion")

If Not pDllGetVersion Is Nothing Then

Dim dvi As DLLVERSIONINFO

Dim hr As Integer

dvi.cbSize = Marshal.SizeOf(dvi)

hr = pDllGetVersion.Invoke(dvi)

If SUCCEEDED(hr) Then

dwVersion = PACKVERSION(dvi.dwMajorVersion, dvi.dwMinorVersion)

End If

End If

FreeLibrary(hinstDll)

End If

Return dwVersion

End Function

~

When I call this function, an error box pops out:

An unhandled exception of type 'System.ArgumentException' occurred in
WinApp.exe
Additional information: äÅÌÅÇÁÔ ÎÅ ÓÏÚÄÁÌ ÕËÁÚÁÔÅÌØ ÎÁ ÆÕÎËÃÉÀ.
(Translation: Delegate haven't created the function pointer.)

If I declare GetProcAddress as Integer it returns normal value, so it's
probably a marshalling problem.
Can I do something to get a delegate value?

Thanks in advance.
Nov 21 '05 #1
8 1328
>Can I do something to get a delegate value?

No, in .NET v1.x this is not supported.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
> No, in .NET v1.x this is not supported.

Bad, bad, too bad... 8=[
Is there another approach?
Nov 21 '05 #3
Is there another approach?


Here's one way

http://www.msjogren.net/dotnet/eng/s...dynpinvoke.asp

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #4
Thanks for great sample. Though, my actual question was: can I execute
function whose address was given by API function.

By the way, can I define a ByRef parameter using this MethodBuilder
approach, or I should define it as IntPtr and use GCHandles for getting an
address?

Thanks
Nov 21 '05 #5
>Thanks for great sample. Though, my actual question was: can I execute
function whose address was given by API function.
Not in VB.NET v7.x you can't. Other languages such as C++ and IL
assembler lets you do it though. And in v2.0 of the framework there's
a way to create a delegate for an arbitrary functon pointer.

By the way, can I define a ByRef parameter using this MethodBuilder
approach, or I should define it as IntPtr and use GCHandles for getting an
address?


Yes you can, but to create a ByRef Type in v1.x of the framework you
have to pass the typename with a "&" appended to
System.Type.GetType().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #6
Thanks Mattias.

Unfortunately I can't get the ByRef parameter to pass value back. I call

Dim RetVal As Integer = CInt(t.InvokeMember("DllGetVersion",
BindingFlags.InvokeMethod, Nothing, Activator.CreateInstance(t), New
Object() {dvi})),
where t is Type object.

RetVal equals to 0, so function succeeds, but dvi (DLLVERSIONINFO structure)
doesn't have any members changed.

How can I get it passed?

Thanks again.

"Mattias Sjögren" <ma********************@mvps.org> ???????/???????? ?
???????? ?????????: news:eG**************@tk2msftngp13.phx.gbl...
Thanks for great sample. Though, my actual question was: can I execute
function whose address was given by API function.


Not in VB.NET v7.x you can't. Other languages such as C++ and IL
assembler lets you do it though. And in v2.0 of the framework there's
a way to create a delegate for an arbitrary functon pointer.

By the way, can I define a ByRef parameter using this MethodBuilder
approach, or I should define it as IntPtr and use GCHandles for getting anaddress?


Yes you can, but to create a ByRef Type in v1.x of the framework you
have to pass the typename with a "&" appended to
System.Type.GetType().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #7
>Unfortunately I can't get the ByRef parameter to pass value back. I call

Dim RetVal As Integer = CInt(t.InvokeMember("DllGetVersion",
BindingFlags.InvokeMethod, Nothing, Activator.CreateInstance(t), New
Object() {dvi})),
where t is Type object.

RetVal equals to 0, so function succeeds, but dvi (DLLVERSIONINFO structure)
doesn't have any members changed.

How can I get it passed?


If dvi is a strucure variable you have to do

Dim args As New Object() {dvi}
.... t.InvokeMember("DllGetVersion", ..., args)
dvi = CType(args(0), DLLVERSIONINFO)

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #8
It works now!

Big thanks for that and for all your patience.

Roman

"Mattias Sjögren" <ma********************@mvps.org> ???????/???????? ?
???????? ?????????: news:Oy**************@tk2msftngp13.phx.gbl...
Unfortunately I can't get the ByRef parameter to pass value back. I call

Dim RetVal As Integer = CInt(t.InvokeMember("DllGetVersion",
BindingFlags.InvokeMethod, Nothing, Activator.CreateInstance(t), New
Object() {dvi})),
where t is Type object.

RetVal equals to 0, so function succeeds, but dvi (DLLVERSIONINFO structure)doesn't have any members changed.

How can I get it passed?


If dvi is a strucure variable you have to do

Dim args As New Object() {dvi}
... t.InvokeMember("DllGetVersion", ..., args)
dvi = CType(args(0), DLLVERSIONINFO)

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #9

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

Similar topics

4
by: Randell D. | last post by:
Folks I have the following code for my submit button <input type="submit" name="action" value="Save Article" onClick="this.value='Saving...'; performPrePostChecks();"> I have tested...
5
by: Paolo | last post by:
Hello, I have a field used to add date of births, named DOB. I would like to add another field, named it YOB, which automatically returns the year of birth from the previous field. Any help?...
12
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
1
by: wael | last post by:
Hi every one, I am experimenting with "PtInRegion" WinAPI, I want my form to be red if the mouse inside a specific region and yellow if it is out side that region. The problem is my form turns...
7
by: jg | last post by:
or would I have to use C# or C++?
4
by: Henning M | last post by:
Hej All Im relativ new to VB.net and im trying to collect som device information using cfgmgr32.dll I use - Declare Function GetListLength Lib "cfgmgr32.dll" Alias...
5
by: Lee Xuzhang | last post by:
/* from SICP -- Exercise 4.21: ((lambda (n) ((lambda (fact) (fact fact n)) (lambda (ft k) (if (= k 1) 1 (* k (ft ft (- k 1))))))) 10) */
5
by: bcaillet | last post by:
Hi all, A few weeks ago I posted a question about a potential defect in Oracle 11g. Today I have more details on the error. Please contact me if you are an Oracle Support engineer, I will like...
2
by: Surgey | last post by:
Hi, I have the following WINAPI event, int WINAPI WlxLoggedOnSAS(PVOID pWlxContext,DWORD dwSasType,PVOID pReserved) { if (dwSasType == WLX_SAS_TYPE_CTRL_ALT_DEL) { // Add additional code of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.