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

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.dll")]
unsafe public static extern int OpenFile(char* filename);
......
unsafe private void buttonGetDllTypes_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 2220
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.GetBytes.

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.dll")]
unsafe public static extern int OpenFile(char* filename);
.....
unsafe private void buttonGetDllTypes_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_OpenFile(p);
MessageBox.Show("ciao");
}

There is no method Encoding.GetBytes

Can you write me an exemple_ please

On 4 Dic, 10:15, "Morten Wennevik" <MortenWenne...@hotmail.comwrote:
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.GetBytes.

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.dll")]
unsafe public static extern int OpenFile(char* filename);
.....
unsafe private void buttonGetDllTypes_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.Encoding.Default.GetBytes(fileToOpen);

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_OpenFile(p);
MessageBox.Show("ciao");
}

There is no method Encoding.GetBytes

Can you write me an exemple_ please

On 4 Dic, 10:15, "Morten Wennevik" <MortenWenne...@hotmail.comwrote:
>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.GetBytes..

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.dll")]
unsafe public static extern int OpenFile(char* filename);
.....
unsafe private void buttonGetDllTypes_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.googlegr oups.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.dll")]
unsafe public static extern int OpenFile(char* filename);
.....
unsafe private void buttonGetDllTypes_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(fileToOpen);

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(UnmanagedType.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(UnmanagedType.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**********************@j72g2000cwa.g ooglegroups.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.dll")]
unsafe public static extern int OpenFile(char* filename);
.....
unsafe private void buttonGetDllTypes_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(fileToOpen);

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(UnmanagedType.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(UnmanagedType.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
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...
110
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...
3
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. ...
35
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...
16
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...
204
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 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
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...
69
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...
8
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
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:
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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...

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.