473,407 Members | 2,312 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,407 software developers and data experts.

Passing callback pointer from VB .NET to C dll

I'm trying to call an InitS function in a C dll from VB.NET.
The InitS function requires pointers to 2 callback functions (I
think).
I'm getting error when calling the function: Attempted to read or
write protected memory. This is often an indication that other memory
is corrupt.
Anyone have any ideas what is going wrong, and pointers in right
direction would be appreciated.

Many thanks
Dermot

C code
typedef void (/*CALLBACK*/ CALLCONV *ERRORFCT)(const DWORD,const
DWORD);
PORTEE_DLLCV_LB int CALLCONV InitS(FARPROC fpFinsihed,FARPROC
fpError);

My VB.NET attempt

I'm trying to use delegates to get this working:

Public Class Class2
Public Delegate Sub ScanFinishedDelegate(ByVal long1 As Long,
ByVal long2 As Long)
Public Delegate Sub ScannerErrorDelegate(ByVal long1 As Long,
ByVal long2 As Long)
Private Declare Function ScInit Lib "C:\vbcode\InitS.dll" (ByVal
test As ScanFinishedDelegate, ByVal test2 As ScannerErrorDelegate) As
Integer

Sub InitialiseScanner()
Try
Dim delScanFinished As ScanFinishedDelegate = AddressOf
ScanFinishedCallback
Dim delError As ScannerErrorDelegate = AddressOf
ScannerErrorCallback
Class2.ScInit(delScanFinished, delError)
' Error I'm Getting after previous line:
'Attempted to read or write protected memory. This is
often an indication that other memory is corrupt.
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try

End Sub

Private Shared Sub ScanFinishedCallback(ByVal long1 As Long, ByVal
long2 As Long)
' Must return in timely fashion no msgbox's
'MsgBox(string1)
'MsgBox(string2)
Debug.WriteLine(long1)
End Sub
Private Shared Sub ScannerErrorCallback(ByVal long1 As Long, ByVal
long2 As Long)
' Must return in timely fashion no msgbox's
'MsgBox(string1)
'MsgBox(string2)
Debug.WriteLine(long1)
End Sub

End Class

May 8 '07 #1
3 4047
>Public Class Class2
Public Delegate Sub ScanFinishedDelegate(ByVal long1 As Long,
ByVal long2 As Long)
Public Delegate Sub ScannerErrorDelegate(ByVal long1 As Long,
ByVal long2 As Long)
Try changing the parameter types to Integer or UInteger.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 8 '07 #2
On 8 May, 17:34, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
Public Class Class2
Public Delegate Sub ScanFinishedDelegate(ByVal long1 As Long,
ByVal long2 As Long)
Public Delegate Sub ScannerErrorDelegate(ByVal long1 As Long,
ByVal long2 As Long)

Try changing the parameter types to Integer or UInteger.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
Tried changing parameter types, same error message.
Any other ideas?

May 8 '07 #3
>Tried changing parameter types, same error message.
>Any other ideas?
Do you know what CALLCONV resolves to in the C code? Maybe you have a
calling convention mismatch.
Mattias

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

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

Similar topics

0
by: Jeff | last post by:
I'm trying to pass a proxy class instance (SWIG generated) of CClass, to a python callback function from C++. The proxy class instance of CClass is created from a pointer to the C++ class CClass....
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
0
by: Jeffrey B. Holtz | last post by:
Has anyone used the multimedia timere timeSetEvent in C#? I'm trying to use it to get a 1ms accurate timer. All the other implementations are far to inaccurate in their resolution. 1ms Timer =...
1
by: Mitchell Hughes | last post by:
Hi, I have code from a previous project that I've packaged into a dll in c++. I'm now trying to call functions within that c++ dll from my c# code. I've managed to get it to work for ints and...
2
by: TN | last post by:
I have a bit of C code, that creates an instance of a .Net class that has been built as a type library. Everything is working as expected, I can pass strings to methods in the object. What I...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to...
2
by: Subodh | last post by:
Hi All, I want to use a C++ API in a static lib that returns a linked List in C# I am planning to P/Invoke to perform the Interop, I would like to know which way will be better to interop a...
2
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.