473,382 Members | 1,590 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,382 software developers and data experts.

Passing a structure to a C dll in VB .NET

I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows:

typedef struct tag_scanparm
{
short cmd;
short fdc;
WORD dsf;
short boxcar;
short average;
short chan_ena[\4];
short scan_dark;
short correct_dark;
short extrig;
short upper4chan;
float sdat[\4][\2048]
} SCANPARM;

The real problem seems to be with the arrays. Even using attributes and specifying a size when I decalre the structure in VB, I can't get this to work. It has to be passed by reference, and when I call "GetType" as a part of my call to Marshall.StructureToPtr, I get an error message to the effect that my structure cannot be marshalled as an unmanaged type.

Please forgive the fact that I don't have the exact error message handy...I tried getting this to work for about a day, scanning all types of message boards, and finally threw in the towel and created a COM object in C++ that wrapped the calls to this 3rd Party dll. That's OK for me, but for some of our customers who use VB exclusively it would be nice to know how to do this directly from VB...there has to be a way, and I think I was close!

Any help would be greatly appreciated!
Thanks,
Steve

Nov 20 '05 #1
2 4808
You might want to post your VB version of the structure definition. This
should work fine. When defining the structure in VB, make sure to use the
MarshalAs attribute, and specify the unamangedtype as ByValArray (for the
arrays), including the SizeConst and the ArraySubType properties of the
attribute.

Also, you don't need to use Marshal.StructureToPtr. When defining the C DLL
function export in VB, just set the structure parameter to ByRef instead of
ByVal, and you can pass a structure variable of this type directly to the
function.

-Rob Teixeira [MVP]

"Steve Turner" <an*******@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as
follows:
typedef struct tag_scanparm
{
short cmd;
short fdc;
WORD dsf;
short boxcar;
short average;
short chan_ena[\4];
short scan_dark;
short correct_dark;
short extrig;
short upper4chan;
float sdat[\4][\2048]
} SCANPARM;

The real problem seems to be with the arrays. Even using attributes and specifying a size when I decalre the structure in VB, I can't get this to
work. It has to be passed by reference, and when I call "GetType" as a part
of my call to Marshall.StructureToPtr, I get an error message to the effect
that my structure cannot be marshalled as an unmanaged type.
Please forgive the fact that I don't have the exact error message handy...I tried getting this to work for about a day, scanning all types of
message boards, and finally threw in the towel and created a COM object in
C++ that wrapped the calls to this 3rd Party dll. That's OK for me, but for
some of our customers who use VB exclusively it would be nice to know how to
do this directly from VB...there has to be a way, and I think I was close!
Any help would be greatly appreciated!
Thanks,
Steve

Nov 20 '05 #2
I have decalred the following structure

<StructLayout(LayoutKind.Sequential, Pack:=1)>
Public Structure Scanpara
Public cmd As Shor
Public fdc As Shor
Public dsf As Int3
Public boxcar As Shor
Public average As Shor
Public chan_ena() As Shor
Public scan_dark As Shor
Public correct_dark As Shor
Public extrig As Shor
Public upper4chan As Shor
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8192)>
Public sdat() As Singl

Public Sub Initialize(
ReDim chan_ena(3
ReDim sdat(8191
End Su
End Structur
[/code

And the following function declaration

Declare Function OOI_DoScan Lib "OOIDrv32.dll" (ByRef pVal As Scanparam) As Shor

The original function (from C++ Header) looks like this

short EXPORTED OOI_DoScan(SCANPARM FAR *); // perform 1 acquisitio

The function executes just fine, however if you look at my origianl declaration of the C structure, you'll see that what I really want is a 2-D array (4X2048). If I assume that my first row of data is in positions 0-2047 of the returned (sdat) array, it looks like I'm missing one point off of the front and back of that array (2047 has no data, and position 1 does not either..I expect position 0 to have no data...strange behavior of the third party dll). If I write a C++ COM object to do the same call, and use it in the VB .NET program, I do not see this behavior. I am assuming that this is because I am passing in a 1-D array and making possibly erroneous assumptions about the order in which they will be written to

Decalre a 2-D array you say? HA HA HA! If I decalre the stucture in the following way

<StructLayout(LayoutKind.Sequential, Pack:=1)>
Public Structure Scanpara
Public cmd As Shor
Public fdc As Shor
Public dsf As Int3
Public boxcar As Shor
Public average As Shor
Public chan_ena() As Shor
Public scan_dark As Shor
Public correct_dark As Shor
Public extrig As Shor
Public upper4chan As Shor
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8192)>
Public sdat(,) As Singl

Public Sub Initialize(
ReDim chan_ena(3
ReDim sdat(3, 2047
End Su
End Structur

then when I call my scan function, I get the error

System.TypeLoadException: Con not marshal field sdat of the type OOResourceServer.Scanparam: This type can not be marshaled as a structure field

To say the least, I am frustrated. Is my line of work, lab automation, I often have to interact with third party dlls that have complicated structures that need to be passed in by reference, to do things like high data rate scans. Althought there is the workaround of using C++ to develop a COM wrapper, I need to be able to tell VB .NET programmers how to do this...and they may not be C++ programmers themselves. This seems silly...why can't VB .NET understand that I am passing a 2_D array of float values...is that too much to ask?? There is absolutely not material that I have found that addresses this problem...all examples use 1-D arrays

I will stop now, lest I begin ranting (If I haven't already)

Thanks

Stev

----- Rob Teixeira [MVP] wrote: ----

You might want to post your VB version of the structure definition. Thi
should work fine. When defining the structure in VB, make sure to use th
MarshalAs attribute, and specify the unamangedtype as ByValArray (for th
arrays), including the SizeConst and the ArraySubType properties of th
attribute

Also, you don't need to use Marshal.StructureToPtr. When defining the C DL
function export in VB, just set the structure parameter to ByRef instead o
ByVal, and you can pass a structure variable of this type directly to th
function

-Rob Teixeira [MVP

"Steve Turner" <an*******@discussions.microsoft.com> wrote in messag
news:94**********************************@microsof t.com..
I have read several interesting posts on passing structures to C dlls, bu

none seem to cover the following case. The structure (as seen in C) is a
follows:
typedef struct tag_scanparm

{
short cmd;
short fdc;
WORD dsf;
short boxcar;
short average;
short chan_ena[\4];
short scan_dark;
short correct_dark;
short extrig;
short upper4chan;
float sdat[\4][\2048]
} SCANPARM;
The real problem seems to be with the arrays. Even using attributes and specifying a size when I decalre the structure in VB, I can't get this to
work. It has to be passed by reference, and when I call "GetType" as a part
of my call to Marshall.StructureToPtr, I get an error message to the effect
that my structure cannot be marshalled as an unmanaged type. Please forgive the fact that I don't have the exact error message handy...I tried getting this to work for about a day, scanning all types of
message boards, and finally threw in the towel and created a COM object in
C++ that wrapped the calls to this 3rd Party dll. That's OK for me, but for
some of our customers who use VB exclusively it would be nice to know how to
do this directly from VB...there has to be a way, and I think I was close! Any help would be greatly appreciated!
Thanks,
Steve

Nov 20 '05 #3

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

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
15
by: John Alway | last post by:
Hello, I'm using a DLL I wrote in C++, and am attempting to call and use it from VB. This works fine for functions where I pass parameters by value, but I can't get pointers to work. I get...
7
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I...
11
by: Macca | last post by:
Hi, I'm writing an application that will pass a large amount of data between classes/functions. In C++ it was more efficient to send a pointer to the object, e.g structure rather than passing...
3
by: ishwarbg | last post by:
Hi Everyone, I have a .Net Application, through which I am invoking a function from a legacy DLL developed in C++. My structure in C# contains some data of type double which I need to pass to to...
1
by: sharadvasista | last post by:
I have a structure typedef struct t_abcd { int a; int b; int c; } abcd; I have to pass the structure to another function. I can do this in two ways.
7
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.