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

pinvoke double[] to Intptr , passing double as IntPtr ??

Hi

I am struggling with pinvoke

I have dll a function that is declared as :
sf_command (IntPtr sndfile,int command, IntPtr data, int datasize);

i need to pass a pointer to a double in the data parameter, i now did :

IntPtr mem = Marshal.AllocHGlobal(sizeof(double));

sf_command (sndfile,command, mem, sizeof(double));

then i did

byte[] ba=new byte[sizeof (Double)];

ba[0]=Marshal.ReadByte(mem,0);
ba[1]=Marshal.ReadByte(mem,1);
ba[2]=Marshal.ReadByte(mem,2);
ba[3]=Marshal.ReadByte(mem,3);
ba[4] = Marshal.ReadByte(mem, 4);
ba[5] = Marshal.ReadByte(mem, 5);
ba[6] = Marshal.ReadByte(mem, 6);
ba[7] = Marshal.ReadByte(mem, 7);

max_val = BitConverter.ToDouble(ba,0);

Marshal.FreeHGlobal(mem);
this works ,but i think there should be a shorter way ??

second question :
How do i pass an array doubles to my function ?
Johan
Nov 20 '08 #1
4 7531
Sagaert Johan wrote:
Hi

I am struggling with pinvoke

I have dll a function that is declared as :
sf_command (IntPtr sndfile,int command, IntPtr data, int datasize);

i need to pass a pointer to a double in the data parameter, i now did
:
IntPtr mem = Marshal.AllocHGlobal(sizeof(double));

sf_command (sndfile,command, mem, sizeof(double));

then i did

byte[] ba=new byte[sizeof (Double)];

ba[0]=Marshal.ReadByte(mem,0);
ba[1]=Marshal.ReadByte(mem,1);
ba[2]=Marshal.ReadByte(mem,2);
ba[3]=Marshal.ReadByte(mem,3);
ba[4] = Marshal.ReadByte(mem, 4);
ba[5] = Marshal.ReadByte(mem, 5);
ba[6] = Marshal.ReadByte(mem, 6);
ba[7] = Marshal.ReadByte(mem, 7);

max_val = BitConverter.ToDouble(ba,0);

Marshal.FreeHGlobal(mem);
this works ,but i think there should be a shorter way ??

second question :
How do i pass an array doubles to my function ?
Did you try the obvious solution of using either "double[]" or "ref double"
as the type of the argument in the DllImport?
>

Johan

Nov 20 '08 #2
On Nov 20, 11:25*pm, "Sagaert Johan" <REMOVEsagaer...@hotmail.com>
wrote:
Hi

I am struggling with pinvoke

I *have dll a function that is declared as :
sf_command (IntPtr sndfile,int command, IntPtr data, int datasize);

i need to pass a pointer to a double in the data parameter, i now did :

IntPtr mem = Marshal.AllocHGlobal(sizeof(double));

sf_command (sndfile,command, mem, sizeof(double));

then *i did

byte[] ba=new byte[sizeof (Double)];

ba[0]=Marshal.ReadByte(mem,0);
ba[1]=Marshal.ReadByte(mem,1);
ba[2]=Marshal.ReadByte(mem,2);
ba[3]=Marshal.ReadByte(mem,3);
ba[4] = Marshal.ReadByte(mem, 4);
ba[5] = Marshal.ReadByte(mem, 5);
ba[6] = Marshal.ReadByte(mem, 6);
ba[7] = Marshal.ReadByte(mem, 7);

max_val = BitConverter.ToDouble(ba,0);

Marshal.FreeHGlobal(mem);

this works ,but i think there should be a shorter way ??
Yes. For one thing, you can just declare the parameter as double[] in
the P/Invoke declaration for your function (or "ref double[]" if the
function is going to write some data into that array). For another,
you can use pointers:

double* p = (double*)mem;
Console.WriteLine(*p);

of course, this necessitates the use of "/unsafe", but there's nothing
inherently wrong about it. Pointers exist in C# for a reason, so don't
be afraid to use them for interop.

second question :
How do i pass an array doubles to my function ?

Johan
Nov 21 '08 #3
Hi
casting double does not work (IntPtr) double

Changing my pinvoke declaration from IntPtr to ref double is no option,
since in other case i need to pass pointer to other data type too.

I did find this is a cleaner alternative (since a double is in fact a
structure type )

IntPtr mem = Marshal.AllocHGlobal(sizeof(double));

int err = sf_command(Hsoundfile, SFCommand.SFC_CALC_NORM_SIGNAL_MAX, mem,
sizeof(double)); CheckError(err);

double max_val = (double)Marshal.PtrToStructure(mem, typeof(System.Double));
Marshal.FreeHGlobal(mem); return max_val;

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:u4**************@TK2MSFTNGP03.phx.gbl...
Sagaert Johan wrote:
>Hi

I am struggling with pinvoke

I have dll a function that is declared as :
sf_command (IntPtr sndfile,int command, IntPtr data, int datasize);

i need to pass a pointer to a double in the data parameter, i now did
:
IntPtr mem = Marshal.AllocHGlobal(sizeof(double));

sf_command (sndfile,command, mem, sizeof(double));

then i did

byte[] ba=new byte[sizeof (Double)];

ba[0]=Marshal.ReadByte(mem,0);
ba[1]=Marshal.ReadByte(mem,1);
ba[2]=Marshal.ReadByte(mem,2);
ba[3]=Marshal.ReadByte(mem,3);
ba[4] = Marshal.ReadByte(mem, 4);
ba[5] = Marshal.ReadByte(mem, 5);
ba[6] = Marshal.ReadByte(mem, 6);
ba[7] = Marshal.ReadByte(mem, 7);

max_val = BitConverter.ToDouble(ba,0);

Marshal.FreeHGlobal(mem);
this works ,but i think there should be a shorter way ??

second question :
How do i pass an array doubles to my function ?

Did you try the obvious solution of using either "double[]" or "ref
double" as the type of the argument in the DllImport?
>>

Johan


Nov 21 '08 #4


"Sagaert Johan" <RE*************@hotmail.comwrote in message
news:Oy**************@TK2MSFTNGP04.phx.gbl...
Hi
casting double does not work (IntPtr) double

Changing my pinvoke declaration from IntPtr to ref double is no option,
since in other case i need to pass pointer to other data type too.
You can have multiple p/invoke declarations for the same function with
different argument lists, just like any overloaded function.
Nov 21 '08 #5

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

Similar topics

10
by: Jacek Dziedzic | last post by:
Hi! Let's say I have a class called Triplet that serves as an envelope for double, ie. class Triplet { public: Triplet() {/*...*/} /* some things that double doesn't have, like
16
by: ferran | last post by:
How can I cast from (long double*) to (const double*) I have tried: const double* Value1 = (const double*)Value2; The compiler does not complain but the actual results when I access the...
3
by: Sascha Herpers | last post by:
Hi, I wrote a c dll with a type library to use it in vb. No problem, everything works fine. Now I needed to pass an array of type double to the dll. I defined the function in the type...
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...
3
by: Web learner | last post by:
The following code works fine private List<double> GetDataFor(string column, int selectedYear) { ------- ------- return list; } foreach (double item in GetDataFor("AirTemp", selectedYear))...
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...
2
by: puzzlecracker | last post by:
f(int** x) { } int main (){ int array;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.