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

Trying to declare an extern function

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
Jun 27 '07 #1
4 7562
On Jun 27, 3:46 pm, Rich...@nospam.nospam <Rich...@nospam.nospam>
wrote:

<snip>
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

Jun 27 '07 #2
Ri*****@nospam.nospam wrote:
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.

--
Tom Spink
University of Edinburgh
Jun 27 '07 #3
Hi,

<Ri*****@nospam.nospamwrote in message
news:57**********************************@microsof t.com...
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?

Jun 27 '07 #4
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

<Ri*****@nospam.nospamwrote in message
news:57**********************************@microsof t.com...
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

Jun 27 '07 #5

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

Similar topics

6
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
1
by: Richard A. Lowe | last post by:
I'm successfully using SendInput to emulate mouse events for a legacy app, delcared like so: public extern static int SendInput( int theCount, ref INPUT pInputs, int theSize ); From the...
7
by: Kobu | last post by:
The code below isn't compiling for me (error message: conflicting types for 'total' - pointing to the extern declaration). Why wouldn't this work, since the types are different, the extern...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
9
by: Dave | last post by:
I am trying to call VerQueryValue from a C# program. VerQueryValue takes as one of its parameters a pointer to a pointer to an array of bytes, which it uses to return a pointer to the required...
4
by: izhar.wallach | last post by:
Hi all, I've noticed an odd behavior of the linker while trying to compile the code below. The linking did not fail even though variable a1 (A2.cpp) was defined as extern in namespace A2 while...
1
by: kid_kei | last post by:
Say there are two classes: System_Controller_Class and UDP_Input_Class. If I were to declare an instance of class UDP_Input_Class in System_Controller_Class like so: extern UDP_Input_Class ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.