473,503 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Pointers.. "int*" anyone?

Ok
let me explain: I am writing a c# program that calls into
an unmanaged C++ third-party DLL. I have to make a number
of calls and for simplification the protype I am calling
is:
bool fn(int *pResult);

This result is used by other subsequent functions that I
have to call.
Q: Is it possible to call this function from C# declaring
a variable that is a pointer which can be used later on to
pass as an (int *)?

I have done it using the <unsafe> declaration, but our
*architecture* team dont like it. I have tried with the
Reflection.Pointers and also __makeref(), but these dont
give me what I need.

Anyone got any ideas!
Thanks in anticipation
Stu
Nov 15 '05 #1
5 7304
Stuart <St**********@nationwide.co.uk> wrote:
let me explain: I am writing a c# program that calls into
an unmanaged C++ third-party DLL. I have to make a number
of calls and for simplification the protype I am calling
is:
bool fn(int *pResult);

This result is used by other subsequent functions that I
have to call.
Q: Is it possible to call this function from C# declaring
a variable that is a pointer which can be used later on to
pass as an (int *)?


Have you tried passing an int by ref instead?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Yup... tried bool foo(ref int)

All the time I get a compiler error which effectively
states:
"Cannot convert ref int to int*"

Incidentally:
I *can* do this with code that is marked unsafe... but the
powers at be (i.e. the architecture bods) dont like this
(tbh: i'm not sure if they dont understand, or just *fear*
the unsafe keyword for an aesthetic reason.)

My (limited) understanding is that effectively I am trying
to convince the c# compiler to pass the address of an int
around - something that c# can not inherently do as the
gac randomly moves memory - hence the *pinning* or
*fixing* of variables in the *unsafe* sections. As
c#doesn't have *pointers*, by definition, any code that
declares them is effectively syntactically incorrect
unless explicitly marked as unsafe.

Therefore is this banging my head against a brick wall?
:)

I've even thought of custom marshalling (yuk!)
Stu

-----Original Message-----
Stuart <St**********@nationwide.co.uk> wrote:
let me explain: I am writing a c# program that calls into an unmanaged C++ third-party DLL. I have to make a number of calls and for simplification the protype I am calling is:
bool fn(int *pResult);

This result is used by other subsequent functions that I have to call.
Q: Is it possible to call this function from C# declaring a variable that is a pointer which can be used later on to pass as an (int *)?


Have you tried passing an int by ref instead?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 15 '05 #3
Stu,

The reason you are getting the compiler error is that you have to remove
the unsafe declaration so that it is not there anymore. For example, your
original definition looked like this:

[DllImport("some.dll")]
public extern unsafe bool fn(int *pResult);

And the call looked like this:

unsafe
{
// Make the call.
int pintResult = 0;
fn(&pintResult);
}

Or something to that effect.

Your declaration should look like this:

[DllImport("some.dll")]
public extern bool fn(ref int pResult);

And the call should look like this:

// The result.
int pintResult = 0;

// Make the call.
fn(ref pintResult);

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

<an*******@discussions.microsoft.com> wrote in message
news:06****************************@phx.gbl...
Yup... tried bool foo(ref int)

All the time I get a compiler error which effectively
states:
"Cannot convert ref int to int*"

Incidentally:
I *can* do this with code that is marked unsafe... but the
powers at be (i.e. the architecture bods) dont like this
(tbh: i'm not sure if they dont understand, or just *fear*
the unsafe keyword for an aesthetic reason.)

My (limited) understanding is that effectively I am trying
to convince the c# compiler to pass the address of an int
around - something that c# can not inherently do as the
gac randomly moves memory - hence the *pinning* or
*fixing* of variables in the *unsafe* sections. As
c#doesn't have *pointers*, by definition, any code that
declares them is effectively syntactically incorrect
unless explicitly marked as unsafe.

Therefore is this banging my head against a brick wall?
:)

I've even thought of custom marshalling (yuk!)
Stu

-----Original Message-----
Stuart <St**********@nationwide.co.uk> wrote:
let me explain: I am writing a c# program that calls into an unmanaged C++ third-party DLL. I have to make a number of calls and for simplification the protype I am calling is:
bool fn(int *pResult);

This result is used by other subsequent functions that I have to call.
Q: Is it possible to call this function from C# declaring a variable that is a pointer which can be used later on to pass as an (int *)?


Have you tried passing an int by ref instead?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 15 '05 #4
Ok,
Firstly apologies! looks like my questions were too
abstracted from the orginal problem. Its my
misunderstanding of the developer's requirements!

The orginal Win32 function is this:

unsigned long
STDCALL findByName(
char *searchString, // pointer to wildcard
search string
long startCategory, // 0
int categoriesOnly, // 0, or 1
long *categoryIDs, // array of long
char categoryNames[][51],// array
int *numCategories, // int (returned count)
long *billerIDs, // array of long
char billerNames[][51], // array
int *numBillers); // int (returned count)

This is exported in a third party DLL. All memory must be
preallocated before call which the function fills in for
you.

In in order to call this, we have to set up and allocate
memory for the required arrays and pass in the pointers.
Invocation of the function fills the arrays and also a the
counts (max elements in all the arrays)

When the arays were returned, C# interpreted them as a
single value types, not as array resulting in some rather
*interesting* code to go through the returned data looking
for null terminators etc.

So is there anyway to *nicely* invoke and manipulate the
returned array information?

Stu
-----Original Message-----
Stu,

The reason you are getting the compiler error is that you have to removethe unsafe declaration so that it is not there anymore. For example, youroriginal definition looked like this:

[DllImport("some.dll")]
public extern unsafe bool fn(int *pResult);

And the call looked like this:

unsafe
{
// Make the call.
int pintResult = 0;
fn(&pintResult);
}

Or something to that effect.

Your declaration should look like this:

[DllImport("some.dll")]
public extern bool fn(ref int pResult);

And the call should look like this:

// The result.
int pintResult = 0;

// Make the call.
fn(ref pintResult);

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

<an*******@discussions.microsoft.com> wrote in message
news:06****************************@phx.gbl...
Yup... tried bool foo(ref int)

All the time I get a compiler error which effectively
states:
"Cannot convert ref int to int*"

Incidentally:
I *can* do this with code that is marked unsafe... but the powers at be (i.e. the architecture bods) dont like this
(tbh: i'm not sure if they dont understand, or just *fear* the unsafe keyword for an aesthetic reason.)

My (limited) understanding is that effectively I am trying to convince the c# compiler to pass the address of an int around - something that c# can not inherently do as the
gac randomly moves memory - hence the *pinning* or
*fixing* of variables in the *unsafe* sections. As
c#doesn't have *pointers*, by definition, any code that
declares them is effectively syntactically incorrect
unless explicitly marked as unsafe.

Therefore is this banging my head against a brick wall?
:)

I've even thought of custom marshalling (yuk!)
Stu

>-----Original Message-----
>Stuart <St**********@nationwide.co.uk> wrote:
>> let me explain: I am writing a c# program that calls

into
>> an unmanaged C++ third-party DLL. I have to make a

number
>> of calls and for simplification the protype I am

calling
>> is:
>> bool fn(int *pResult);
>>
>> This result is used by other subsequent functions
that I
>> have to call.
>> Q: Is it possible to call this function from C#

declaring
>> a variable that is a pointer which can be used later
on to
>> pass as an (int *)?
>
>Have you tried passing an int by ref instead?
>
>--
>Jon Skeet - <sk***@pobox.com>
>http://www.pobox.com/~skeet
>If replying to the group, please do not mail me too
>.
>

.

Nov 15 '05 #5
Stuart,
unsigned long
STDCALL findByName(
char *searchString, // pointer to wildcard
search string
long startCategory, // 0
int categoriesOnly, // 0, or 1
long *categoryIDs, // array of long
char categoryNames[][51],// array
int *numCategories, // int (returned count)
long *billerIDs, // array of long
char billerNames[][51], // array
int *numBillers); // int (returned count)


Try declaring it like this

[DllImport("Your.dll")]
static extern uint findByName(
string searchString,
int startCategory,
int categoriesOnly,
int[] categoryIDs,
IntPtr[] categoryNames,
out int numCategories,
int[] billerIDs,
IntPtr[] billerNames,
out int numBillers);

And then for every valid pointer (IntPtr) you get back in the arrays,
call Marshal.PtrToStringAnsi().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #6

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

Similar topics

4
1615
by: Joe Peterson | last post by:
I could not find another example of this via internet searches, so here it is... I am wondering if this is a python bug or otherwise. The first example of this happened in a larger program of...
3
12337
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
24
2319
by: Apotheosis | last post by:
The problem professor gave us is: Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the...
134
8915
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
4
6042
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple...
12
8390
by: hn.ft.pris | last post by:
I know int *p mean an array of pointers to int, but I meet int (*p) which is confusing. What does it mean and how to use it? Thanks for help.
6
5305
by: Lawrence Spector | last post by:
I ran into a problem using g++. Visual Studio 2005 never complained about this, but with g++ I ran into this error. I can't figure out if I've done something wrong or if this is a compiler bug. ...
6
2684
by: Warly girl | last post by:
Hi i want your help again in c++ !! i want a function change the "int" to "string" because in my project "Registration system" in task "b" i must write a course class which contains number of...
13
2771
by: Anna | last post by:
I try to put 8 int bit for example 10100010 into one character of type char(1 octet) with no hope . Could anyone propose a simple way to do it? Thank you very much.
0
7199
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,...
0
7076
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
7274
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,...
1
6984
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...
0
7453
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...
0
5576
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,...
1
5005
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...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
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...

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.