|
-
Apr 1st, 2004, 08:41 AM
#1
Thread Starter
New Member
passing data between C++ dll and vb.net
I have a function in a C++ dll defined as
Code:
FMSIO_API BOOL SetLogData( const char *dbName, int id, int boreholeID, int nameID, float startDepth, float depthSampleRate, int values_NUM_PTS, float * logData, const char *apiCode "UNKNOWN", const char *runDate "00:00:00");
im trying to figure out how to pass the data for the logData element..
I declare
Code:
<DllImport("c:\windows\system32\FMSIO.dll", EntryPoint:="#35", CallingConvention:=CallingConvention.Cdecl)> Public Shared Function SetLogData(ByVal dbName As String, ByVal id As Int32, ByVal boreholeID As Int32, ByVal nameID As Int32, ByVal startDepth As Single, ByVal depthSampleRate As Single, ByVal values_NUM_PTS As Int32, ByVal logdata() As Single) As Int32
and then try
Code:
Dim logdataout() As Single
Dim logdataptr As System.IntPtr
logdataout(0) = 5
logdataout(1) = 4
logdataout(2) = 5
logdataout(3) = 5
logdataout(4) = 4
t = s.SetLogData("C:\FMS2_build.mdb", 6724, 1006, 1407, 100, 0.1524, 5, logdataout())
but this just results in an error:
Code:
An unhandled exception of type 'System.NullReferenceException' occurred in FMSLoad.exe
Additional information: Object reference not set to an instance of an object.
so I guess I am passing the data the wrong way - can anyone correct the above and show me how to pass an array of singles/floats by value like this?
-
Apr 1st, 2004, 04:10 PM
#2
Frenzied Member
Not sure about your function call, but I think the reason you're getting not set to instance business is because logdataout is Nothing when you try to assign it values.
Change to Dim logdataout(4) As Single or something.
Mike
-
Apr 5th, 2004, 02:11 AM
#3
Junior Member
Try ByRef
Since the c++ function expects logdata as a pointer, maybe you should try using something similar to
Code:
ByRef logdata As Single
in the DllImport statement in order to pass a reference (pointer) to you data array.
Maybe you'll still need the parentheses, I don't know for sure. But try it out and let me know if it works. I will have to do exactly the same thing next week, except that I must use my c++ dll under VB6...
-
Apr 5th, 2004, 05:05 AM
#4
Thread Starter
New Member
the null reference exception was fixed by Dim logdataout(4) as single, thanks
.. however I still can't get this to work, I don't get any error messages or crashes but the dll returns with a value of zero and the data isn't written to the database as it should be.. I can't be sure wether this is because I am calling the DLL incorrectly or there is a problem within the DLL itself though
I tried passing ByRef logdata As Single in the DllImport but that made no difference (except that i then had to call it with logdataout(0) for the program to compile)
-
Apr 5th, 2004, 09:26 AM
#5
Frenzied Member
Wish I could help, but have no idea what fmsio is - can't find in my windows directories. Maybe you can get a sample from the vendor?
-
Apr 6th, 2004, 02:05 AM
#6
Junior Member
How I did it
I was working on something similar last year. Maybe this can help you out. I had created an MFC dll in VC++.NET, from which I was exporting this function
Code:
extern "C" _declspec (dllexport) void RunFilter(char* DataPointer, ...
... long DataSize, float* Result)
where DataPointer is a char pointer to input data, DataSize is length of data and Result is an fixed length array (171 elements) used for returning output from the dll.
In VB.NET i first declare a fixed length array and then I use the Alias command to use the function in my VB application.
[Edit: The reason that the array is defined as VBFixedArray is that the call to the dll function otherwise may alter the length of the array, corrupting the data that you want to return.]
VB Code:
'Declare Single-array of fixed length (171)
<VBFixedArray(171), System.Runtime.InteropServices.MarshalAs(...
... Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=171)>...
... Public Resultat As Single()
'Variables used by other functions
Public DataPointer As Byte()
Public DataSize As Integer
'Give my function the name ScanFilter
Public Declare Sub ScanFilter Lib "ScanFilter.dll" Alias "RunFilter" (...
... ByVal DataPointer As Byte(), ByVal DataSize As Integer, ByVal Resultat As Single())
Finally, in order to execute the DLL function I initialized DataPointer and DataSize and then used the following commands:
VB Code:
'Variabel declared in my alias function
Resultat = New Single(171) {}
'Run the filter
ScanFilter(DataPointer, DataSize, Resultat)
Last edited by jskog; Apr 7th, 2004 at 09:06 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|