Trying to declare an extern function

G

Guest

I'm trying to declare CreateFile as an extern from the dll like so:

....
using System.Runtime.InteropServices;

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
internal static extern IntPtr CreateFile(
String filename,
UInt32 desiredAccess,
UInt32 shareMode,
IntPtr attributes, // Really, this is the SECURITY_ATTRIBUTES pointer
UInt32 creationDisposition,
UInt32 flagsAndAttributes,
IntPtr templateFile);


namespace CsharpPipes
{
....

I get an error on the "IntPtr" that says:
error CS1518: Expected class, delegate, enum, interface, or struct

with F1 help text of:
A declaration was found that is not supported in a namespace. Inside a
namespace, the compiler accepts only classes, structs, enums, interfaces,
namespaces, and delegates.

I assumed my declaration was outside a namespace(?) What's the proper
syntax to declare CreateFile?

Thanks in advance.

Richard
 
J

Jon Skeet [C# MVP]

On Jun 27, 3:46 pm, (e-mail address removed) <[email protected]>
wrote:

I assumed my declaration was outside a namespace(?) What's the proper
syntax to declare CreateFile?

Your assumption is incorrect - your declaration should be within a
class, like any other method declaration.

Jon
 
T

Tom Spink

I'm trying to declare CreateFile as an extern from the dll like so:

...
using System.Runtime.InteropServices;

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
internal static extern IntPtr CreateFile(
String filename,
UInt32 desiredAccess,
UInt32 shareMode,
IntPtr attributes, // Really, this is the SECURITY_ATTRIBUTES pointer
UInt32 creationDisposition,
UInt32 flagsAndAttributes,
IntPtr templateFile);


namespace CsharpPipes
{
...

I get an error on the "IntPtr" that says:
error CS1518: Expected class, delegate, enum, interface, or struct

with F1 help text of:
A declaration was found that is not supported in a namespace. Inside a
namespace, the compiler accepts only classes, structs, enums, interfaces,
namespaces, and delegates.

I assumed my declaration was outside a namespace(?) What's the proper
syntax to declare CreateFile?

Thanks in advance.

Richard

Hi Richard,

Your declaration is indeed inside a namespace, and further more, the
declaration *must* reside within a class.

You need to put your declaration inside a class definition.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I'm trying to declare CreateFile as an extern from the dll like so:

...
using System.Runtime.InteropServices;

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
internal static extern IntPtr CreateFile(
String filename,
UInt32 desiredAccess,
UInt32 shareMode,
IntPtr attributes, // Really, this is the SECURITY_ATTRIBUTES pointer
UInt32 creationDisposition,
UInt32 flagsAndAttributes,
IntPtr templateFile);


namespace CsharpPipes
{
...

I get an error on the "IntPtr" that says:
error CS1518: Expected class, delegate, enum, interface, or struct

with F1 help text of:
A declaration was found that is not supported in a namespace. Inside a
namespace, the compiler accepts only classes, structs, enums, interfaces,
namespaces, and delegates.

I assumed my declaration was outside a namespace(?) What's the proper
syntax to declare CreateFile?

You have to declare it as a member of a class.

Also what you need to P/invoke CreateFile for?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Just after clickiing send I saw the name of the class and that give the idea
you were trying to use Named Pipes.

If so google for some already built frameworks.

Also IIRC Pipes will be supported in the next version of the framework
 

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

Top