473,657 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems calling a C function containing pointers.

I need to call a C DLL function. The first parameter expects a pointer
to a long. It returns a value at the address of the pointer. The second
parameter expects a pointer to a pointer. It creates an array and
returns the data it stores in this array.

I have tried many different combinations of P/Invoke declarations and
none have worked. When I step from my c# code into the c code, oNumRays
points to 0x00000000 and when it attempts to assign a value, an error
gets generated (something about object not set). Here are the
declarations I have tried:

public static extern Char SomeFunction(re f int numberOfRays, IntPtr
rayDirs);
public static extern Char SomeFunction(ou t int numberOfRays, IntPtr
rayDirs);
public static extern Char SomeFunction(In tPtr numberOfRays, IntPtr
rayDirs);
public static extern Char SomeFunction([MarshalAs(Unman agedType.U4)]
out int numberOfRays, IntPtr rayDirs);
public static extern Char SomeFunction([MarshalAs(Unman agedType.U4)]
int numberOfRays, IntPtr rayDirs);
Here is the C function...

static float sRayDirs[8];

GuiInterface_AP I char SomeFunction(lo ng* oNumRays, float** oRayDirs)
{
*oNumRays = 12;

sRayDirs[0] = 1.1f;
sRayDirs[1] = 2.2f;
sRayDirs[2] = 3.3f;
sRayDirs[3] = 4.4f;
sRayDirs[4] = 5.5f;
sRayDirs[5] = 6.6f;
sRayDirs[6] = 7.7f;
sRayDirs[7] = 8.8f;

*oRayDirs = sRayDirs;

return 0;
}

Any suggestions on how to make this work?

Thanks for your help!
Johann Blake

Nov 17 '05 #1
8 1428
Johann,

The first declaration should work, but you have to correct the return
type:

public static extern byte SomeFunction(re f int number ofRays, IntPtr
rayDirs);

The reason for this is that a char in .NET is a 16-bit value, whereas in
C it is an 8-bit value.

You just have to make sure that you allocate the memory for the array
which the IntPtr points to before you make the call.

If you can use unsafe code, then you can simplify a lot of this by just
declaring the function with pointers, and then declare a pointer to the
array.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Johann Blake" <jo*********@ya hoo.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I need to call a C DLL function. The first parameter expects a pointer
to a long. It returns a value at the address of the pointer. The second
parameter expects a pointer to a pointer. It creates an array and
returns the data it stores in this array.

I have tried many different combinations of P/Invoke declarations and
none have worked. When I step from my c# code into the c code, oNumRays
points to 0x00000000 and when it attempts to assign a value, an error
gets generated (something about object not set). Here are the
declarations I have tried:

public static extern Char SomeFunction(re f int numberOfRays, IntPtr
rayDirs);
public static extern Char SomeFunction(ou t int numberOfRays, IntPtr
rayDirs);
public static extern Char SomeFunction(In tPtr numberOfRays, IntPtr
rayDirs);
public static extern Char SomeFunction([MarshalAs(Unman agedType.U4)]
out int numberOfRays, IntPtr rayDirs);
public static extern Char SomeFunction([MarshalAs(Unman agedType.U4)]
int numberOfRays, IntPtr rayDirs);
Here is the C function...

static float sRayDirs[8];

GuiInterface_AP I char SomeFunction(lo ng* oNumRays, float** oRayDirs)
{
*oNumRays = 12;

sRayDirs[0] = 1.1f;
sRayDirs[1] = 2.2f;
sRayDirs[2] = 3.3f;
sRayDirs[3] = 4.4f;
sRayDirs[4] = 5.5f;
sRayDirs[5] = 6.6f;
sRayDirs[6] = 7.7f;
sRayDirs[7] = 8.8f;

*oRayDirs = sRayDirs;

return 0;
}

Any suggestions on how to make this work?

Thanks for your help!
Johann Blake

Nov 17 '05 #2
Hi Nicholas,

I did correct the return type and used the first declaration I showed,
but it does not work and neither does using unsafe marked code.

Any other suggestions?

Thanks
Johann

Nov 17 '05 #3
Johann,

You need to show how you are calling it, as the declarations are
correct.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Johann Blake" <jo*********@ya hoo.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hi Nicholas,

I did correct the return type and used the first declaration I showed,
but it does not work and neither does using unsafe marked code.

Any other suggestions?

Thanks
Johann

Nov 17 '05 #4

"Johann Blake" <jo*********@ya hoo.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I need to call a C DLL function. The first parameter expects a pointer
to a long. It returns a value at the address of the pointer. The second
parameter expects a pointer to a pointer. It creates an array and
returns the data it stores in this array.

I have tried many different combinations of P/Invoke declarations and
none have worked. When I step from my c# code into the c code, oNumRays
points to 0x00000000 and when it attempts to assign a value, an error
gets generated (something about object not set). Here are the
declarations I have tried:

public static extern Char SomeFunction(re f int numberOfRays, IntPtr
rayDirs);
public static extern Char SomeFunction(ou t int numberOfRays, IntPtr
rayDirs);
public static extern Char SomeFunction(In tPtr numberOfRays, IntPtr
rayDirs);
public static extern Char SomeFunction([MarshalAs(Unman agedType.U4)]
out int numberOfRays, IntPtr rayDirs);
public static extern Char SomeFunction([MarshalAs(Unman agedType.U4)]
int numberOfRays, IntPtr rayDirs);
Here is the C function...

static float sRayDirs[8];

GuiInterface_AP I char SomeFunction(lo ng* oNumRays, float** oRayDirs)
{
*oNumRays = 12;

sRayDirs[0] = 1.1f;
sRayDirs[1] = 2.2f;
sRayDirs[2] = 3.3f;
sRayDirs[3] = 4.4f;
sRayDirs[4] = 5.5f;
sRayDirs[5] = 6.6f;
sRayDirs[6] = 7.7f;
sRayDirs[7] = 8.8f;

*oRayDirs = sRayDirs;

return 0;
}

Any suggestions on how to make this work?

Thanks for your help!
Johann Blake


Your signature is not correct, 1) the return type should be a byte, 2) the
arrary pointer must be passed by ref.

Try this...

public static extern byte SomeFunction(re f int numberOfRays, ref IntPtr
rayDirs);

The float array must be marshaled to a managed float array, this has to be
done in code. V2.0 makes it a lot easier because it has a Marshal.Copy
overload that copies an unmanaged float array to a managed float array.

Here is how you can marshal an unmanaged float array to managed float array
....

byte b = SomeFunction(re f int numberOfRays, ref IntPtr rayDirs);

int unmPtr = rayDirs.ToInt32 (); // non portable to 64 bit!
int sizeOfFloatArra yBytes = numberOfRays* 4 ; // 4 = sizeof(float)
byte[] bar = new byte[sizeOfFloatArra yBytes ];
for (int i = 0 ; i < sizeOfFloatArra yBytes ; i++ )
{
bar[i] = Marshal.ReadByt e(new IntPtr(unmPtr++ ));
}
float[] far = new float[numberOfRays];
for (int n = 0; n < numberOfRays; n++)
{
far[n] = BitConverter.To Single(bar, n * 4);
}
foreach(float f in far )
Console.WriteLi ne("{0}",f);

Willy.
Nov 17 '05 #5
Nicholas,

I managed to get it to work. Don't ask me what I did that made it work.
The original C signature had a few more parameters preceding the ones
that I didn't post here. They were basic data types and not pointers. I
removed them and added them one at a time and verified that everything
was getting passed correctly. Eventually after adding each one, it all
worked, so I don't know what caused the problem.

As for the return data type being a bool, that turns out to be
incorrect. .NET returns a true if the C function returns 0. Willy's
post below is correct in that a byte is required. Not sure why .NET
returns a True when the function returns 0.

Thanks again,
Johann

Nov 17 '05 #6
You're right. Surprisingly, a bool does not work as Nicholas suggested.
A bool returns true when the C function returns zero, which is wrong.

Johann

Nov 17 '05 #7

"Johann Blake" <jo*********@ya hoo.com> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
You're right. Surprisingly, a bool does not work as Nicholas suggested.
A bool returns true when the C function returns zero, which is wrong.

Johann


I don't see any mention of a bool in your original posting, but if you are
talking about an API returning a bool, you need to know that C might have
different ways to express a bool, and you need to declare the function to
return a bool not a char or byte.

Note that the issue with your declaration was the missing ref for the second
argument, did it solve the problem?

public static extern byte SomeFunction(re f int numberOfRays, ref IntPtr
rayDirs);

or if the return is a 'bool' (C intrinsic type bool is one byte)

[return:MarshalA s(UnmanagedType .U1)]
public static extern bool SomeFunction(re f int numberOfRays, ref IntPtr
rayDirs);

or:

public static extern bool SomeFunction(re f int numberOfRays, ref IntPtr
rayDirs);
if the return type is a BOOL or BOOLEAN (typedefs in C)

Willy.
Nov 17 '05 #8
The problem disappeared for some unknown reason. The ref was actually
the first thing I used. As for the return value, when the C function
returns a char and I use a bool, a returned value of 0 from C will
definitely result in a true in .NET. I have a feeling that C somehow
exports the return type as text and possibly converts a numerical 0 to
a character "0". Just my guess. But the correct solution is to use a
byte on the .NET side.

Johann

Nov 17 '05 #9

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

Similar topics

1
1979
by: Lyn Hartley | last post by:
I wan't to send a string containing XML to a Microsoft XML gateway. I'm using the include code below as an include and calling the function with somthing like the calling code. I have sent the streem to a port listener and all is fine but when its sent to the gateway I just get a Incorrect RequestType. I'm just wondering if anyone knows what I'm missing or doing wrong, I can find an abundance if info on parceing XML but idealy I just...
6
1579
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to an abstract base class OptionBase. OptionBase is not templated but I am deriving a template class called Option from OptionBase. The class Option has a public member function called get_value() that is not present in the class OptionBase. My problem is that when calling get_value() I have to cast to Option<some_type>* and I was wondering if I can somehow remove the cast so I dont have to...
3
3788
by: kohvirus | last post by:
Well after embaressing myself and posting in the wrong fourm, I found my way to the right one and I'm hoping to seek some help. This is the program without any modifications that I am making recently (because I'll most likely make it blow up moreso). I'm currently trying to write a program with an array but for the life of me I can't seem to get the right syntax for the array. Here is the program as follows: #include <iostream>
1
2199
by: S?ren Gammelmark | last post by:
Hi I have been searching the web and comp.lang.c of a method of using variable-argument function pointers and the like. And some of the questions arising in this post are answered partly in these posts, but I ask mainly for a way to found a way to solve this problem. I'm a hobbyist so there might be an alltogether better solution to the problem, so please come with any solution you might think of. I'm open to ideas. --- And i'm not...
5
2450
by: Simon Harris | last post by:
Hi All, I am trying to call a method in a web form, from an event fired in a user control. My user control displays a map, which has a link button to enlarge/shrink the map. When the user enlarges the map, I want to hide my navigation table etc, maximising the viewing area. I've been working on this for 5 hours now, so far I have as detailed below - Which rund with out error, but the final function never gets called. Any help/suggestions...
4
3519
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The objective in utilizing this new deployment method is to reduce the maintenance overhead as well as making it easier for my users to setup and run the application initially. I have VS 2002, Windows XP, Access XP(2000 format). He is my problem....
18
4342
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
9
1687
by: bwaichu | last post by:
I am starting this thread after having run into two separate programming problems, where the compiler offered no help. The compiler did not even warn. The programs compiled fine. And the programs even appeared to work. The first case was passing directly a variable of type char **. Here's an example:
14
1700
by: stevenruiz | last post by:
Hello All My question mainly is how to use/reference Double Pointers? I am currently trying to understand what the meaning of a 'vector of pointers' means also? What I am trying to do is take a char array and break it up into words omitting the spaces. What needs to be noted is that I am trying to accomplish this only using char ** and char *. Therefore, I am creating it from scratch. Below is code that I have written so far:
0
8397
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
8310
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
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2731
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
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.