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. 8 1328
> No, in .NET v1.x this is not supported.
Bad, bad, too bad... 8=[
Is there another approach?
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
>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.
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.
>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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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?...
|
by: junky_fellow |
last post by:
How can I declare a function that returns a pointer
to one dimensional array ?
|
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...
|
by: jg |
last post by:
or would I have to use C# or C++?
|
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...
|
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)
*/
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
| |