Api functions

G

Guest

Hello, I wanted to ask what are API functions, how do they differ from the
regular functions and how can I use them in C#/visual C#. ( please give an
example if you can)
Thanks a lot.
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

API is a general term that can be used in a number of different ways.
For example, the framework itself can be considered an API. However, most
of the time, when one says "Windows API" (which is how I use the term), it
means the functions exported by the system OS dlls (kernel32, user32,
advapi, etc, etc).

Hope this helps.
 
G

Guest

Thanks.
But very function I use I get the compile time error:
Invalid expression term '['
; expected
Invalid expression temr ']'
and I see a red underline under the "[DllImport("kernel32.dll")]
for example when I try to use the api function CreateDirectory.
What's missing?
PS: I have included the line "using System.Runtime.InteropServices;" if that
helps
PS2:the structure of the api function is:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

Where are you placing the declaration? Is it outside of a class? You
need to place the declaration inside of a class declaration in order for it
to compile correctly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Thanks.
But very function I use I get the compile time error:
Invalid expression term '['
; expected
Invalid expression temr ']'
and I see a red underline under the "[DllImport("kernel32.dll")]
for example when I try to use the api function CreateDirectory.
What's missing?
PS: I have included the line "using System.Runtime.InteropServices;" if
that
helps
PS2:the structure of the api function is:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);

Philip Rieck said:
In addition to what Nicholas has already said, if you're looking at how
to
use them from c#,
see: http://pinvoke.net/
 
G

Guest

OK thanks again, it helped but now I have another problem:
I put this declaration inside the class:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);
static bool B;
and when I use this
B= CreateDirectory("Directory",null);
it says: The best overloeaded method match for CreateDirectory(string,
System.IntPtr has some invalid arguments
and Argument '2':cannot convert from < null> to System.IntPtr'

I put null since I have no idea what this second argument means and how to
create a IntPtr, and what is it anyway?
Thanks again


Nicholas Paldino said:
Michael,

Where are you placing the declaration? Is it outside of a class? You
need to place the declaration inside of a class declaration in order for it
to compile correctly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Thanks.
But very function I use I get the compile time error:
Invalid expression term '['
; expected
Invalid expression temr ']'
and I see a red underline under the "[DllImport("kernel32.dll")]
for example when I try to use the api function CreateDirectory.
What's missing?
PS: I have included the line "using System.Runtime.InteropServices;" if
that
helps
PS2:the structure of the api function is:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);

Philip Rieck said:
In addition to what Nicholas has already said, if you're looking at how
to
use them from c#,
see: http://pinvoke.net/



Hello, I wanted to ask what are API functions, how do they differ from
the
regular functions and how can I use them in C#/visual C#. ( please give
an
example if you can)
Thanks a lot.
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

An IntPtr is a value type, and you need to pass IntPtr.Zero to represent
a null pointer.

Also, why not just call the static CreateDirectory method on the
Directory class in the System.IO namespace?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
OK thanks again, it helped but now I have another problem:
I put this declaration inside the class:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);
static bool B;
and when I use this
B= CreateDirectory("Directory",null);
it says: The best overloeaded method match for CreateDirectory(string,
System.IntPtr has some invalid arguments
and Argument '2':cannot convert from < null> to System.IntPtr'

I put null since I have no idea what this second argument means and how to
create a IntPtr, and what is it anyway?
Thanks again


Nicholas Paldino said:
Michael,

Where are you placing the declaration? Is it outside of a class?
You
need to place the declaration inside of a class declaration in order for
it
to compile correctly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Thanks.
But very function I use I get the compile time error:
Invalid expression term '['
; expected
Invalid expression temr ']'
and I see a red underline under the "[DllImport("kernel32.dll")]
for example when I try to use the api function CreateDirectory.
What's missing?
PS: I have included the line "using System.Runtime.InteropServices;" if
that
helps
PS2:the structure of the api function is:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);

:

In addition to what Nicholas has already said, if you're looking at
how
to
use them from c#,
see: http://pinvoke.net/



Hello, I wanted to ask what are API functions, how do they differ
from
the
regular functions and how can I use them in C#/visual C#. ( please
give
an
example if you can)
Thanks a lot.
 
G

Guest

Oh great now it works, well first of all I thought IntPtr is like a pointer
to integer in C++ so that's why I thought using a null is more appropriate.
And secondly I just tried this simple function to understand how to use API
functions...
As a matter of fact I have been seeking for functions through which I can
access the ports of the pc; The parallel port, serial port, the usb port and
so on, I just couldn't find a function in the Net framework which can allow
me to send a binary info or recieve it, through these physical ports.
Do you know of any functions or dlls where I can find such api functions?
Thanks a lot for your help!


Nicholas Paldino said:
Michael,

An IntPtr is a value type, and you need to pass IntPtr.Zero to represent
a null pointer.

Also, why not just call the static CreateDirectory method on the
Directory class in the System.IO namespace?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
OK thanks again, it helped but now I have another problem:
I put this declaration inside the class:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);
static bool B;
and when I use this
B= CreateDirectory("Directory",null);
it says: The best overloeaded method match for CreateDirectory(string,
System.IntPtr has some invalid arguments
and Argument '2':cannot convert from < null> to System.IntPtr'

I put null since I have no idea what this second argument means and how to
create a IntPtr, and what is it anyway?
Thanks again


Nicholas Paldino said:
Michael,

Where are you placing the declaration? Is it outside of a class?
You
need to place the declaration inside of a class declaration in order for
it
to compile correctly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks.
But very function I use I get the compile time error:
Invalid expression term '['
; expected
Invalid expression temr ']'
and I see a red underline under the "[DllImport("kernel32.dll")]
for example when I try to use the api function CreateDirectory.
What's missing?
PS: I have included the line "using System.Runtime.InteropServices;" if
that
helps
PS2:the structure of the api function is:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);

:

In addition to what Nicholas has already said, if you're looking at
how
to
use them from c#,
see: http://pinvoke.net/



Hello, I wanted to ask what are API functions, how do they differ
from
the
regular functions and how can I use them in C#/visual C#. ( please
give
an
example if you can)
Thanks a lot.
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

In order to access the serial port, you are going to have to call
CreateFile (through the P/Invoke layer), and then pass the handle to a
FileStream object.

As for the parallel and USB ports, I don't know of any interface that
you can use (you will have to use interop though, definitely).

.NET 2.0 has a new namespace, System.IO.Ports which has classes to
access the serial ports.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Oh great now it works, well first of all I thought IntPtr is like a
pointer
to integer in C++ so that's why I thought using a null is more
appropriate.
And secondly I just tried this simple function to understand how to use
API
functions...
As a matter of fact I have been seeking for functions through which I can
access the ports of the pc; The parallel port, serial port, the usb port
and
so on, I just couldn't find a function in the Net framework which can
allow
me to send a binary info or recieve it, through these physical ports.
Do you know of any functions or dlls where I can find such api functions?
Thanks a lot for your help!


Nicholas Paldino said:
Michael,

An IntPtr is a value type, and you need to pass IntPtr.Zero to
represent
a null pointer.

Also, why not just call the static CreateDirectory method on the
Directory class in the System.IO namespace?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
OK thanks again, it helped but now I have another problem:
I put this declaration inside the class:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);
static bool B;
and when I use this
B= CreateDirectory("Directory",null);
it says: The best overloeaded method match for CreateDirectory(string,
System.IntPtr has some invalid arguments
and Argument '2':cannot convert from < null> to System.IntPtr'

I put null since I have no idea what this second argument means and how
to
create a IntPtr, and what is it anyway?
Thanks again


:

Michael,

Where are you placing the declaration? Is it outside of a class?
You
need to place the declaration inside of a class declaration in order
for
it
to compile correctly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks.
But very function I use I get the compile time error:
Invalid expression term '['
; expected
Invalid expression temr ']'
and I see a red underline under the "[DllImport("kernel32.dll")]
for example when I try to use the api function CreateDirectory.
What's missing?
PS: I have included the line "using System.Runtime.InteropServices;"
if
that
helps
PS2:the structure of the api function is:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);

:

In addition to what Nicholas has already said, if you're looking at
how
to
use them from c#,
see: http://pinvoke.net/



Hello, I wanted to ask what are API functions, how do they differ
from
the
regular functions and how can I use them in C#/visual C#. (
please
give
an
example if you can)
Thanks a lot.
 
G

Guest

ok.
Thanks a lot!
have a good day

Nicholas Paldino said:
Michael,

In order to access the serial port, you are going to have to call
CreateFile (through the P/Invoke layer), and then pass the handle to a
FileStream object.

As for the parallel and USB ports, I don't know of any interface that
you can use (you will have to use interop though, definitely).

.NET 2.0 has a new namespace, System.IO.Ports which has classes to
access the serial ports.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Oh great now it works, well first of all I thought IntPtr is like a
pointer
to integer in C++ so that's why I thought using a null is more
appropriate.
And secondly I just tried this simple function to understand how to use
API
functions...
As a matter of fact I have been seeking for functions through which I can
access the ports of the pc; The parallel port, serial port, the usb port
and
so on, I just couldn't find a function in the Net framework which can
allow
me to send a binary info or recieve it, through these physical ports.
Do you know of any functions or dlls where I can find such api functions?
Thanks a lot for your help!


Nicholas Paldino said:
Michael,

An IntPtr is a value type, and you need to pass IntPtr.Zero to
represent
a null pointer.

Also, why not just call the static CreateDirectory method on the
Directory class in the System.IO namespace?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

OK thanks again, it helped but now I have another problem:
I put this declaration inside the class:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);
static bool B;
and when I use this
B= CreateDirectory("Directory",null);
it says: The best overloeaded method match for CreateDirectory(string,
System.IntPtr has some invalid arguments
and Argument '2':cannot convert from < null> to System.IntPtr'

I put null since I have no idea what this second argument means and how
to
create a IntPtr, and what is it anyway?
Thanks again


:

Michael,

Where are you placing the declaration? Is it outside of a class?
You
need to place the declaration inside of a class declaration in order
for
it
to compile correctly.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks.
But very function I use I get the compile time error:
Invalid expression term '['
; expected
Invalid expression temr ']'
and I see a red underline under the "[DllImport("kernel32.dll")]
for example when I try to use the api function CreateDirectory.
What's missing?
PS: I have included the line "using System.Runtime.InteropServices;"
if
that
helps
PS2:the structure of the api function is:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);

:

In addition to what Nicholas has already said, if you're looking at
how
to
use them from c#,
see: http://pinvoke.net/



Hello, I wanted to ask what are API functions, how do they differ
from
the
regular functions and how can I use them in C#/visual C#. (
please
give
an
example if you can)
Thanks a lot.
 
N

Nick Malik

Hi Michael,

Please post enough of the code for us to see the error.

It is possible that you are placing the attribute declaration inside a
method? If so, move the declaration outside the method (but within the
class).

--- Nick

Michael said:
Thanks.
But very function I use I get the compile time error:
Invalid expression term '['
; expected
Invalid expression temr ']'
and I see a red underline under the "[DllImport("kernel32.dll")]
for example when I try to use the api function CreateDirectory.
What's missing?
PS: I have included the line "using System.Runtime.InteropServices;" if that
helps
PS2:the structure of the api function is:
[DllImport("kernel32.dll")]
static extern bool CreateDirectory(string lpPathName,
IntPtr lpSecurityAttributes);

Philip Rieck said:
In addition to what Nicholas has already said, if you're looking at how to
use them from c#,
see: http://pinvoke.net/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top