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

passing address of a Managed float array to unamaged DLL

I need to pass an address of a Managed float array to a DLL. The
following doesn't seem to work
[DllImportAttribute("xx32_.DLL", CharSet=CharSet.Auto)]
extern static float GetXXX([MarshalAs(UnmanagedType.LPStr)]
StringBuilder HWND,
[MarshalAs(UnmanagedType.U8)] long nWhat,
[MarshalAs(UnmanagedType.LPArray)] ref float[] lparam
);

.......
float[] fMeasValues = new float[256];
ifRtn = GetXXX(null, WAVELENGTH, ref fWaveLengths);

Regards,

Dan

Nov 17 '05 #1
3 2877
Dan,

What is the original declaration of the function?

Also, you won't need the CharSet property in the DllImport attribute if
you are specifying the type of the string as LPStr specifically.

If you want a pointer to the array, then you can just use what you have.

However, if you want the pointer to the pointer which is the array
itself, then you will need to use IntPtr and marshal yourself, like this:

[DllImportAttribute("xx32_.DLL", CharSet=CharSet.Auto)]
extern static float GetXXX(
[MarshalAs(UnmanagedType.LPStr)]
StringBuilder HWND,
[MarshalAs(UnmanagedType.U8)] long nWhat,
[MarshalAs(UnmanagedType.LPArray)] ref IntPtr lParam);
The reason for this is that if you need a double-pointer, then your
array is being allocated in the function itself. You need the value of the
pointer upon return so that you can free the memory (since the P/Invoke
layer doesn't know how it is allocated, it can't de-allocate it).

Then, to get your float array, you start with the lParam parameter
(which is set to the array beginning now), and use pointer arithmetic to
cycle through the memory address, and marshal the values back to you.

You should post the declaration of the function in unmanaged code, as I
am looking at your parameters, and they seem rather odd. For example, you
are using a StringBuilder for a parameter named HWND. Now, it may be that
this parameter is a string, and named HWND, but typically, I see that for
windows handles, not strings, which makes me think that there is something
much more amiss.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"dbru" <d.*******@comcast.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I need to pass an address of a Managed float array to a DLL. The
following doesn't seem to work
[DllImportAttribute("xx32_.DLL", CharSet=CharSet.Auto)]
extern static float GetXXX([MarshalAs(UnmanagedType.LPStr)]
StringBuilder HWND,
[MarshalAs(UnmanagedType.U8)] long nWhat,
[MarshalAs(UnmanagedType.LPArray)] ref float[] lparam
);

.......
float[] fMeasValues = new float[256];
ifRtn = GetXXX(null, WAVELENGTH, ref fWaveLengths);

Regards,

Dan

Nov 17 '05 #2
Nicholas:

Here's the DLL declaration:
DLLEXPORT float WINAPI GetXXX (HWND, long, LPARAM);

The reason I use StringBuilder as a parameter is that I usually pass
null in the first paramter and if I code it as int HWND it complains
about converting<null> to int.

Thanks for the help!

Regards,

Dan

Nov 17 '05 #3
Dan,

That's not what you should declare this as then.

Your declaration should be:

[DllImportAttribute("xx32_.DLL", CharSet=CharSet.Auto)]
extern static float GetXXX(
IntPtr hWnd,
int nWhat,
IntPtr lParam);

A long in C++ is a 32-bit integer, while in C#, it is a 64-bit integer.
This definitely screws up your call.

Also, you should use IntPtr, and pass IntPtr.Zero for the first
parameter.

Finally, you didn't answer what the third parameter is. An LPARAM is
just an IntPtr, but you don't specify if the array returned is created by
the function, or if it is the responsibility of the user to allocate it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"dbru" <d.*******@comcast.net> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Nicholas:

Here's the DLL declaration:
DLLEXPORT float WINAPI GetXXX (HWND, long, LPARAM);

The reason I use StringBuilder as a parameter is that I usually pass
null in the first paramter and if I code it as int HWND it complains
about converting<null> to int.

Thanks for the help!

Regards,

Dan

Nov 17 '05 #4

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

Similar topics

1
by: R BUckshaw | last post by:
Hello, I have been attempting to write a VB class that would expose its stuff to an older (ok, legacy application) program which can load and call exported functions from the old style dlls. ...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: JohnO | last post by:
I am new to CppNet How can I pass a double or float value to Unmanaged function from Managed CppNet Program. I can pass an integer with no problem but when I try it with double or float I get the...
2
by: The unProfessional | last post by:
Any know how to convert a managed array to an unmanaged array (ptr)? // Managed float f = new float ; // Unmanaged unsafe { float *pArray = f; // No good
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
2
by: sonaliagr | last post by:
I am trying to update a msg array in function by passing the address but it is showing an error. and also, i want the value of msg array to be accessible to the full code that is inside the main...
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
5
by: mshaaban | last post by:
Hello, In my code I have a large static 2D arrays defined as: code: #define LONMAX 1440 #define LATMAX60 480 void main (int argc, char *argv)
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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...
0
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...
0
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,...

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.