DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

VB.NET wrapper for writing DIAdem files

I'm trying to write a wrapper class in VB.NET 2005 for the DIAdem Connectivity Library to write Diadem files within .NET code.
For example I've been successfull with

(Declaration)

Declare

Function GetLibraryErrorDescription Lib "nilibddc.dll" Alias "DDC_GetLibraryErrorDescription" _
(ByVal errorCode As Integer) As IntPtr

(Call)

Dim s as IntPtr = DIAdemConnLibWrapper.GetLibraryErrorDescription(deb)
MsgBox(Marshal.PtrToStringAnsi(s))      'output: error message

But after spending hours with the following declaration I am quite desperate:

(Declaration)

Declare Ansi Function CreateFile Lib "nilibddc.dll" Alias "DDC_CreateFile" _
ByVal filePath As String, _
ByVal fileType As String, _
ByVal name As String, _
ByVal description As String, _
ByVal title As String, _
ByVal author As String, _
ByVal DDCFileHandle As IntPtr) As Integer

(Call)

result = DIAdemConnLibWrapper.CreateFile(str1, str2, str3, str4, str5, str6, myIntPtr)

The arguments (filename and so on) are correct, because using them in a native C-Application works very well. After this call, result is -6202, saying there were invalid arguments. I've tried Ansi/Auto/Unicode in the declaration, StringBuilder as type (even when strings should be OK, according to the MSDN), ByRef/ByVal and different marshalling instructions like <MarshalAs(UnmanagedType.LPWStr)>. I always get the same error.

Has anyone ever done a successfull wrapping of the DLL in .NET? What's wrong with my code?

0 Kudos
Message 1 of 7
(5,590 Views)
Hello Stjag!
 
Just change
 
ByVal DDCFileHandle As IntPtr
 
to
 
ByRef DDCFileHandle As IntPtr
 
because it is an output parameter.
 
Matthias
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 2 of 7
(5,584 Views)
Thank you,
now the file has been created physically. The next problem is that

Declare

Ansi Function DDC_SaveFile Lib "nilibddc.dll" _
(
ByRef DDCFileHandle As IntPtr) As Integer

or any other function needing a filehandle returns -6202 ( "An invalid file handle was passed to the library. ")  although the filehandle IntPtr gets changed by DDC_CreateFile!

0 Kudos
Message 3 of 7
(5,562 Views)
Hello Stjag!
 
It is a good idea to have a look at the C declarations in 'nilibddc.h' before converting and undestand the difference between values/pointers and ByVal/ByRef
 
For DDC_CreateFile it looks like this:
int __stdcall DDC_CreateFile (const char *filePath,
                              const char *fileType,
         const char *name,
         const char *description,
         const char *title,
         const char *author,
         DDCFileHandle *file);
This results in VB.NET in:
Declare Ansi Function CreateFile Lib "nilibddc.dll" Alias "DDC_CreateFile" _
ByVal filePath As String, _
ByVal fileType As String, _
ByVal name As String, _
ByVal description As String, _
ByVal title As String, _
ByVal author As String, _
ByRef DDCFileHandle As IntPtr) As Integer
For DDC_SaveFile it is:
int __stdcall DDC_SaveFile (DDCFileHandle file);
This sholud result in this VB.NET Code:
Declare Ansi Function SaveFile Lib "nilibddc.dll" Alias "DDC_SaveFile" _
(ByVal DDCFileHandle As IntPtr) As Integer
 
 
It is essential that DDC_CreateFile changes the handle because this is the identfier for the specific file! It is allowed to open several file at a time. Therfore you have to give e.g. to DDC_SaveFile an identifier to the file you want to save.
 
Matthias
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 4 of 7
(5,554 Views)

Thank you very much for your explanation, it's working now! Smiley Happy

0 Kudos
Message 5 of 7
(5,549 Views)

Hello I have most of the nilibddc.dll working with VB.NET but I do not get any data saved to my TDMS after calling DDC_SetDataValues.

Are there any good samples for doing this from VB.NET?

I create the Group, Channel, then pass a pointer to an array of double to DDC_SetDataValues.  But my TDMS file has 0 points saved under the channel and I get no errors.

 

Declare Ansi Function SetDataValues Lib "nilibddc.dll" Alias "DDC_SetDataValues" _
(ByVal channelHandle As IntPtr,
ByVal values As IntPtr,
ByVal numValues As UIntPtr) As Integer 'numbvalues is size_t

 

''The call

Dim values() As Double = {1, 2, 3, 4, 5}

Dim iptr As IntPtr
iptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(Of Double) * values.Length)
Marshal.Copy(values, 0, iptr, values.Length)
result = SetDataValues(channelHandle, iptr, 5)

0 Kudos
Message 6 of 7
(1,676 Views)


This article is really amazing. Thanks for the sharing. HCA Rewards Login

0 Kudos
Message 7 of 7
(1,641 Views)