Unmanaged Code: Need help defining output parameters

Funger

Member
Joined
Dec 15, 2006
Messages
8
Programming Experience
5-10
Hi all! I have been beating my head against the wall for a couple of days now, and I was hoping that one of the wizards here would be able to help me solve my problem.

I have an unmanaged dll written in C++ that I need to access from VB.NET 2.0.

This is the definition of the function in C++.
VB.NET:
extern "C" __declspec(dllimport) int LogGet(int* pLogNumber, char** LogNames, char** LogTimes);

In VB.NET I imported this function with the following:
VB.NET:
	<DllImport("CPPDLL.dll")> Public Shared Function _
	LogGet(<Out()> ByRef pLogNumber As Integer, _
	<Out()> ByVal LogNames() As String, _
	<Out()> ByVal LogTimes() As String) As Integer
	End Function

The following is how I am trying to call the imported function. I know that it is incorrect, but at least it's somewhere to start. For simplicity, I'm not doing anything with the output parameters in my example. I'm just trying to get the call to execute at this point.
VB.NET:
	Public Sub LogGetTest()
		Dim LogNumbers As Integer
		Dim LogNames() As String = Nothing
		Dim LogTimes() As String = Nothing
		Dim Success As Integer

		Success = CayenComm_LogGet(LogNumbers, LogNames, LogTimes)
	End Sub

All three of the parameters are output parameters, and I have no idea how big the resulting arrays are going to be when I call the function. When I execute the code as written, I get the following error:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Please help! I don't know what else to do. Thanks in advance!

-Jim
 
You have declared your second and third parameters as String arrays, which I don't think is right. A char* is basically an unmanaged string, so a char** will be a pointer to a string. I think you'd have to declare your method like this:
VB.NET:
<DllImport("CPPDLL.dll")> _
Public Shared Function LogGet(<Out()> ByRef pLogNumber As Integer, _
                              <Out()> ByRef LogNames As String, _
                              <Out()> ByRef LogTimes As String) As Integer
End Function
That said, I've not ever tried to call a function with a similar signature so I'm not 100% sure.
 
Back
Top