473,748 Members | 8,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer

Hi, friends. I am new in this group and I have a problem. I am just
started to program in cSharp and I need a help.
I need to import a ddl and to use one of its function. The function, in
C language, require in input a pointer char:

OpenFile(char* filename);

To use this function in cSharp I have writing the following code:
=============== =============== ===============
[DllImport("My.d ll")]
unsafe public static extern int OpenFile(char* filename);
......
unsafe private void buttonGetDllTyp es_Click(object sender, EventArgs e)
{
fileToOpen = "C:\\myFile.ext ";
fixed (char* p = fileToOpen)
{
OpenFile(p);
}
}
=============== =============== ===============

But it doesn't work. I don't know if this function is deprecated or
there is a problem in the calling him. When I build or run my project
there is not any error.
I want to know the list of functions in the dll. Can I retrieve these
from the dll?

Can someone Help me. Thanks

Dec 4 '06 #1
5 2240
Hi Sheikko,

A C/C++ char is the equivalent of a byte in C#. Try changing your char*
to byte*. To get a byte array out of a string, use Encoding.GetByt es.

On Mon, 04 Dec 2006 09:31:24 +0100, Sheikko <sh*****@gmail. comwrote:
Hi, friends. I am new in this group and I have a problem. I am just
started to program in cSharp and I need a help.
I need to import a ddl and to use one of its function. The function, in
C language, require in input a pointer char:

OpenFile(char* filename);

To use this function in cSharp I have writing the following code:
=============== =============== ===============
[DllImport("My.d ll")]
unsafe public static extern int OpenFile(char* filename);
.....
unsafe private void buttonGetDllTyp es_Click(object sender, EventArgs e)
{
fileToOpen = "C:\\myFile.ext ";
fixed (char* p = fileToOpen)
{
OpenFile(p);
}
}
=============== =============== ===============

But it doesn't work. I don't know if this function is deprecated or
there is a problem in the calling him. When I build or run my project
there is not any error.
I want to know the list of functions in the dll. Can I retrieve these
from the dll?

Can someone Help me. Thanks


--
Happy Coding!
Morten Wennevik [C# MVP]
Dec 4 '06 #2
Thank you very much for the answer.
I have changed the code, but it still not work

fixed (byte* p = fileToOpen)
{
CompeGPSPort_Op enFile(p);
MessageBox.Show ("ciao");
}

There is no method Encoding.GetByt es

Can you write me an exemple_ please

On 4 Dic, 10:15, "Morten Wennevik" <MortenWenne... @hotmail.comwro te:
Hi Sheikko,

A C/C++ char is the equivalent of a byte in C#. Try changing your char*
to byte*. To get a byte array out of a string, use Encoding.GetByt es.

On Mon, 04 Dec 2006 09:31:24 +0100, Sheikko <shei...@gmail. comwrote:
Hi, friends. I am new in this group and I have a problem. I am just
started to program in cSharp and I need a help.
I need to import a ddl and to use one of its function. The function, in
C language, require in input a pointer char:
OpenFile(char* filename);
To use this function in cSharp I have writing the following code:
=============== =============== ===============
[DllImport("My.d ll")]
unsafe public static extern int OpenFile(char* filename);
.....
unsafe private void buttonGetDllTyp es_Click(object sender, EventArgs e)
{
fileToOpen = "C:\\myFile.ext ";
fixed (char* p = fileToOpen)
{
OpenFile(p);
}
}
=============== =============== ===============
But it doesn't work. I don't know if this function is deprecated or
there is a problem in the calling him. When I build or run my project
there is not any error.
I want to know the list of functions in the dll. Can I retrieve these
from the dll?
Can someone Help me. Thanks--
Happy Coding!
Morten Wennevik [C# MVP]
Dec 4 '06 #3

byte[] bytes = System.Text.Enc oding.Default.G etBytes(fileToO pen);

byte* p = bytes;

You may also want to try a pointer to the string using the 'fixed'
keyword. The article below describes some advanced pinvoking using the
compact framework, but the principles are the same for the full framework.

http://msdn.microsoft.com/library/de...advinterop.asp
On Mon, 04 Dec 2006 10:39:51 +0100, Sheikko <sh*****@gmail. comwrote:
Thank you very much for the answer.
I have changed the code, but it still not work

fixed (byte* p = fileToOpen)
{
CompeGPSPort_Op enFile(p);
MessageBox.Show ("ciao");
}

There is no method Encoding.GetByt es

Can you write me an exemple_ please

On 4 Dic, 10:15, "Morten Wennevik" <MortenWenne... @hotmail.comwro te:
>Hi Sheikko,

A C/C++ char is the equivalent of a byte in C#. Try changing your char*
to byte*. To get a byte array out of a string, use Encoding.GetByt es..

On Mon, 04 Dec 2006 09:31:24 +0100, Sheikko <shei...@gmail. comwrote:
Hi, friends. I am new in this group and I have a problem. I am just
started to program in cSharp and I need a help.
I need to import a ddl and to use one of its function. The function,
in
C language, require in input a pointer char:
OpenFile(char* filename);
To use this function in cSharp I have writing the following code:
=============== =============== ===============
[DllImport("My.d ll")]
unsafe public static extern int OpenFile(char* filename);
.....
unsafe private void buttonGetDllTyp es_Click(object sender, EventArgs
e)
{
fileToOpen = "C:\\myFile.ext ";
fixed (char* p = fileToOpen)
{
OpenFile(p);
}
}
=============== =============== ===============
But it doesn't work. I don't know if this function is deprecated or
there is a problem in the calling him. When I build or run my project
there is not any error.
I want to know the list of functions in the dll. Can I retrieve these
from the dll?
Can someone Help me. Thanks--
Happy Coding!
Morten Wennevik [C# MVP]


--
Happy Coding!
Morten Wennevik [C# MVP]
Dec 4 '06 #4
"Sheikko" <sh*****@gmail. comwrote in message
news:11******** **************@ j72g2000cwa.goo glegroups.com.. .
Hi, friends. I am new in this group and I have a problem. I am just
started to program in cSharp and I need a help.
I need to import a ddl and to use one of its function. The function, in
C language, require in input a pointer char:

OpenFile(char* filename);

To use this function in cSharp I have writing the following code:
=============== =============== ===============
[DllImport("My.d ll")]
unsafe public static extern int OpenFile(char* filename);
.....
unsafe private void buttonGetDllTyp es_Click(object sender, EventArgs e)
{
fileToOpen = "C:\\myFile.ext ";
fixed (char* p = fileToOpen)
{
OpenFile(p);
}
}
=============== =============== ===============

But it doesn't work. I don't know if this function is deprecated or
there is a problem in the calling him. When I build or run my project
there is not any error.
I want to know the list of functions in the dll. Can I retrieve these
from the dll?

Can someone Help me. Thanks

Well, you are new to CSharp and you start treating C# as if it was C, you will miss your
start because C# isn't the same as C.
First , you should stay away from unsafe constructs whenever possible. Second, you should
stay away from interop features like PInvoke, at least when you start learning the language
and (more importantly) the framework.
To achieve the first goal, remove the unsafe from signature (DllImport) and change the
argument as follows:
public static extern int OpenFile(string filename);

and, change your code into:
fileToOpen = "C:\\myFile.ext ";
OpenFile(fileTo Open);

this leaves you with one common issue, that is - what is a char* in C -.
What is it pointing to exactly, is it pointing to a "Unicode character" array or is it
pointing to a "Single byte character" array or is it pointing to something else?
You should know that a 'char' in C# is Unicode encoded, that is' it's a 'wide' character,
and a string in .NET is an array of char's. This means that somehow you need to indicate to
the PInvoke layer what the native code char* looks like, if you don't give an hint, PInvoke
considers char* a pointer to a null terminated "single byte character" array. Indicating
what the destination points to, can be done by using the MarshalAs attribute. For instance
if the native code expects a pointer to a "Wide character" array, you have to apply the
following attribute to the argument, like :

public static extern int OpenFile([MarshalAs(Unman agedType.LPWStr )] string filename);
if it's pointing to a single byte character array, you can change the signature into this:
public static extern int OpenFile([MarshalAs(Unman agedType.LPStr)] string filename);

The second goal can be achieved by using the framework classes instead of calling into
unmanaged code, in the above case, it's just a matter of using the FileStream classes in
order to access files on disk.

Please consult the docs for details on other possible marshaling types and consult the docs
whenever you dive into interop or whatever. Don't start coding in C# like you would in C,
read the docs, leverage the framework classes first, learn the C# language and forget about
C (coding styles) and it's libraries.

Another remark, you declared the OpenFile to return an int
1) are you sure it returns an int?
2)why don't you use the return value in your code?
If you don't have access to the documentation of the library functions you are calling, you
will have to guess both the arguments and return types, IMO in this case you shouldn't call
into the library at all.

And last but not least, please be more explicit when posting questions like this, post the
error message(s), stack traces etc....
"But it doesn't work. " isn't of any help

Willy.
Dec 4 '06 #5
I have solved The problem. THank you.

On 4 Dic, 11:42, "Willy Denoyette [MVP]" <willy.denoye.. .@telenet.be>
wrote:
"Sheikko" <shei...@gmail. comwrote in messagenews:11* *************** ******@j72g2000 cwa.googlegroup s.com...
Hi, friends. I am new in this group and I have a problem. I am just
started to program in cSharp and I need a help.
I need to import a ddl and to use one of its function. The function, in
C language, require in input a pointer char:
OpenFile(char* filename);
To use this function in cSharp I have writing the following code:
=============== =============== ===============
[DllImport("My.d ll")]
unsafe public static extern int OpenFile(char* filename);
.....
unsafe private void buttonGetDllTyp es_Click(object sender, EventArgs e)
{
fileToOpen = "C:\\myFile.ext ";
fixed (char* p = fileToOpen)
{
OpenFile(p);
}
}
=============== =============== ===============
But it doesn't work. I don't know if this function is deprecated or
there is a problem in the calling him. When I build or run my project
there is not any error.
I want to know the list of functions in the dll. Can I retrieve these
from the dll?
Can someone Help me. ThanksWell, you are new to CSharp and you start treating C# as if it was C, you will miss your
start because C# isn't the same as C.
First , you should stay away from unsafe constructs whenever possible. Second, you should
stay away from interop features like PInvoke, at least when you start learning the language
and (more importantly) the framework.
To achieve the first goal, remove the unsafe from signature (DllImport) and change the
argument as follows:
public static extern int OpenFile(string filename);

and, change your code into:
fileToOpen = "C:\\myFile.ext ";
OpenFile(fileTo Open);

this leaves you with one common issue, that is - what is a char* in C -.
What is it pointing to exactly, is it pointing to a "Unicode character" array or is it
pointing to a "Single byte character" array or is it pointing to something else?
You should know that a 'char' in C# is Unicode encoded, that is' it's a 'wide' character,
and a string in .NET is an array of char's. This means that somehow you need to indicate to
the PInvoke layer what the native code char* looks like, if you don't give an hint, PInvoke
considers char* a pointer to a null terminated "single byte character" array. Indicating
what the destination points to, can be done by using the MarshalAs attribute. For instance
if the native code expects a pointer to a "Wide character" array, you have to apply the
following attribute to the argument, like :

public static extern int OpenFile([MarshalAs(Unman agedType.LPWStr )] string filename);
if it's pointing to a single byte character array, you can change the signature into this:
public static extern int OpenFile([MarshalAs(Unman agedType.LPStr)] string filename);

The second goal can be achieved by using the framework classes instead of calling into
unmanaged code, in the above case, it's just a matter of using the FileStream classes in
order to access files on disk.

Please consult the docs for details on other possible marshaling types and consult the docs
whenever you dive into interop or whatever. Don't start coding in C# like you would in C,
read the docs, leverage the framework classes first, learn the C# language and forget about
C (coding styles) and it's libraries.

Another remark, you declared the OpenFile to return an int
1) are you sure it returns an int?
2)why don't you use the return value in your code?
If you don't have access to the documentation of the library functions you are calling, you
will have to guess both the arguments and return types, IMO in this case you shouldn't call
into the library at all.

And last but not least, please be more explicit when posting questions like this, post the
error message(s), stack traces etc....
"But it doesn't work. " isn't of any help

Willy.
Dec 4 '06 #6

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

Similar topics

4
2139
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived classes
110
9945
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
3
2361
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
35
2899
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
16
2305
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and back again without change;...
204
13060
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
16
2512
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
7818
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
69
5587
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
8
2237
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
0
8991
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
9374
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...
0
9249
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
8244
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
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
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...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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
2787
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.