473,795 Members | 2,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to marshal an array of type double for PInvoke?

I am porting a vb library to vb.net. The vb library has an external
call to an unmanaged dll.

Existing code:

//External function declaration
Private Declare Function Uncompress& Lib "COMPR.DLL" (ByVal SrcSt$,
ByVal SrcLen&, Dest As Any, ByVal DestLen&)

//Invocation of external function
lReturn = Uncompress(sCom pressedResult, Len(sCompressed Result),
aResults(ARRAY_ START), ByVal lNewLength)

[where aResults is a double array. The COMPR.Uncompres s function
basically uncompress a string 'sCompressedRes ult' and fills the array
aResults. The array is initialized before calling the external
function]

My understanding is that( Please correct me if I am wrong):
a. The third parameter in declaration 'Dest as Any' means a parameter
of variant type.
b. At invocation, the first element of the double array is passed.
i.e., a pointer to the first element in the double array is passed to
the external function.
c. The Uncompress function expands the compressed data, fills/replaces
the double array with values.
d. The changed values are available to the caller after the Uncompress
function has returned.

Now, I have to call the COMPR.Uncompres s method from Vb.Net.
From browsing the net, I got a lot of information regarding this(

Please correct me if I am wrong):
a. We have to mark the parameter as InAttribute(), Out().
b. We should pin the object so that GC does not change address.
c. We should pass a pointer TO THE FIRST OBJECT OF THE ARRAY to the
native function.
d. Array of type double is non-blittable and so we have to do the
marshaling.

This is the code I wrote:

//External method declaration
<DllImport("COM PR.dll", EntryPoint:="Un Compress",
SetLastError:=T rue, CharSet:=CharSe t.Auto, ExactSpelling:= True,
CallingConventi on:=CallingConv ention.StdCall) > _

Public Shared Function UnCompress(ByVa l compressedResul t As String,
ByVal length As Long, <InAttribute( ), Out(),
MarshalAs(Unman agedType.Struct )> ByRef destinationArra yElement As
Object, ByVal newLength As Long) As Long

End Function

//Invocation
'Pin the object
Dim resultsHandle As GCHandle
resultsHandle = GCHandle.Alloc( aResults(ARRAY_ START),
GCHandleType.Pi nned)
Dim resultsPointer As IntPtr = resultsHandle.A ddrOfPinnedObje ct()

'Invoke
returnValue = UnCompress(sCom pressedResult, Len(sCompressed Result),
resultsPointer, newLength)
My problem is that I get the return value correctly, but the array does
not get changed....I get the same data that is passed to the external
function. I think the changes made by the native function is not
persisting.

There is no problem with any of the parameters - I have checked the
same parameters using the existing vb dll and it works. I would like to
know why I dont get the modified array even though I am passing a
pointer to the first element of the array and am pinning the element.

Is this the correct way to marshal (UnmanagedType. struct) and is this
the correct way to pin object?

Any help would be appreciated,

Jim

Feb 2 '06 #1
0 1744

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

Similar topics

9
2819
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the code so I only have the documentation. I'm trying to call a function called z4date that, according to the docs, returns the date as "an 8-byte character string in the "YYYYMMDD" format". When I run it with this code I've written , I get "Can not...
1
6602
by: Camilo Telles | last post by:
Sirs I have the foolowing structures in C #define POLY_MAX 8 // Número máximo de lados em um polígon typedef struct { // Um vértice do polígon double u, v; // texture space coord double sx, sy; // screen space coord
4
3423
by: TT (Tom Tempelaere) | last post by:
Hi people I have a library built using MSVC6 which exports a method with the following signature void library_method( UINT* sw_mode ) I am trying to use PInvoke to be able to call it from my C# project, but I cannot find a suitable marshal type Is there a type that I can use to marshal a UINT*? If not, are there any other integrated solutions to this problem
0
1708
by: Marek | last post by:
Hi I need to call various functions in a native C++ DLL (FORTRAN eventually too) - passing integers, doubles, (pointers and arrays to both of these as well) and ultimately structures too. I was quite happily proceeding using ModuleBuilder.DefinePInvokeMethod until I started implementing calls to pointer (byref) parameters. The switch to TypeBuilder.DefinePInvokeMethod (the one with the required custom modifiers) has resulted in the...
14
4156
by: Vertilka | last post by:
I need to read binary data file written by C++ program, using my C# application. How do i marshal the bytes i read with my C# code to .NET types. The data is numbers (integers float doubles etc..) and strings. I looked at the Marshal class but did not know how to use it with file. Please add few code lines. Thanks alot
0
3244
by: jim4u | last post by:
I am porting a vb library to vb.net. The vb library has an external call to an unmanaged dll. Existing code: //External function declaration Private Declare Function Uncompress& Lib "COMPR.DLL" (ByVal SrcSt$, ByVal SrcLen&, Dest As Any, ByVal DestLen&) //Invocation of external function
10
2160
by: Neville Lang | last post by:
Hi all, Here is a problem I first struck a while back in the Compact Framework. I am now using the full Framework (v2.0) and wanted to know whether there is anyway around this issue without building separate shim code in another DLL. Here is the layout of the external C DLL function: int thisFunction( double a, int b, double* c)
9
6535
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A structure is defined in the header(.h file) as shown below struct validationResult {
2
7208
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7543
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.