I am trying to develop a wrapper class for the Windows API functions in Visual Studio 2008:
GetOpenFileName
GetSaveFileName
I put together a starter class:
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
using System.Runtime. InteropServices ;
[System.Runtime. InteropServices .StructLayout(L ayoutKind.Seque ntial, CharSet=CharSet .Auto)]
public class OpenFileName
{
int lstructSize;
int hwndOwner;
int hInstance;
string lpstrFilter = null;
string lpstrCustomFilt er = null;
int lMaxCustomFilte r;
int lFilterIndex;
string lpstrFile = null;
int lMaxFile = 0;
string lpstrFiteTitle = null;
int lMaxFileTitle = 0;
string lpstrInitialDir = null;
string lpstrTitle = null;
int lFlags;
ushort nFileOffset;
ushort nFileExtension;
string lpstrDefExt = null;
int lCustData;
int lpfHook;
int lpTemplateName;
}
[DllImport("comd lg32.dll", SetLastError=tr ue, CharSet = CharSet.Auto)]
static extern bool GetOpenFileName ([In, Out] OpenFileName ofn);
[DllImport("comd lg32.dll", SetLastError=tr ue, CharSet = CharSet.Auto)]
static extern bool GetSaveFileName ([In, Out] OpenFileName ofn);
namespace myNameSpace
{
class GetFileNames
{
}
}
The compilier complains:
Error 1 Expected class, delegate, enum, interface, or struct
E:\myNameSpace\ myNameSpace\Get FileNames.cs 33 15 myNameSpace
Error 2 Expected class, delegate, enum, interface, or struct
E:\myNameSpace\ myNameSpace\Get FileNames.cs 33 46 myNameSpace
Error 3 Expected class, delegate, enum, interface, or struct
E:\myNameSpace\ myNameSpace\Get FileNames.cs 35 15 myNameSpace
Error 4 Expected class, delegate, enum, interface, or struct
E:\myNameSpace\ myNameSpace\Get FileNames.cs 35 46 myNameSpace
Error 5 The modifier 'extern' is not valid for this item
E:\myNameSpace\ myNameSpace\Get FileNames.cs 32 70 myNameSpace
Error 6 The modifier 'extern' is not valid for this item
E:\myNameSpace\ myNameSpace\Get FileNames.cs 34 70 myNameSpace
Errors 1 and 3 refer to the bool.qualifier
Errors 2 and 4 refer to the OpenFileName qualifier
Errors 5 and 6 refer to the extern
The DLLImport statements were originally retrieved from pinvoke.net using the PInvoke.net add-in.
They were modified to use the fully qualified System.Runtime. InteropServices .DllImport reference.
What am I doing wrong?
GetOpenFileName
GetSaveFileName
I put together a starter class:
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
using System.Runtime. InteropServices ;
[System.Runtime. InteropServices .StructLayout(L ayoutKind.Seque ntial, CharSet=CharSet .Auto)]
public class OpenFileName
{
int lstructSize;
int hwndOwner;
int hInstance;
string lpstrFilter = null;
string lpstrCustomFilt er = null;
int lMaxCustomFilte r;
int lFilterIndex;
string lpstrFile = null;
int lMaxFile = 0;
string lpstrFiteTitle = null;
int lMaxFileTitle = 0;
string lpstrInitialDir = null;
string lpstrTitle = null;
int lFlags;
ushort nFileOffset;
ushort nFileExtension;
string lpstrDefExt = null;
int lCustData;
int lpfHook;
int lpTemplateName;
}
[DllImport("comd lg32.dll", SetLastError=tr ue, CharSet = CharSet.Auto)]
static extern bool GetOpenFileName ([In, Out] OpenFileName ofn);
[DllImport("comd lg32.dll", SetLastError=tr ue, CharSet = CharSet.Auto)]
static extern bool GetSaveFileName ([In, Out] OpenFileName ofn);
namespace myNameSpace
{
class GetFileNames
{
}
}
The compilier complains:
Error 1 Expected class, delegate, enum, interface, or struct
E:\myNameSpace\ myNameSpace\Get FileNames.cs 33 15 myNameSpace
Error 2 Expected class, delegate, enum, interface, or struct
E:\myNameSpace\ myNameSpace\Get FileNames.cs 33 46 myNameSpace
Error 3 Expected class, delegate, enum, interface, or struct
E:\myNameSpace\ myNameSpace\Get FileNames.cs 35 15 myNameSpace
Error 4 Expected class, delegate, enum, interface, or struct
E:\myNameSpace\ myNameSpace\Get FileNames.cs 35 46 myNameSpace
Error 5 The modifier 'extern' is not valid for this item
E:\myNameSpace\ myNameSpace\Get FileNames.cs 32 70 myNameSpace
Error 6 The modifier 'extern' is not valid for this item
E:\myNameSpace\ myNameSpace\Get FileNames.cs 34 70 myNameSpace
Errors 1 and 3 refer to the bool.qualifier
Errors 2 and 4 refer to the OpenFileName qualifier
Errors 5 and 6 refer to the extern
The DLLImport statements were originally retrieved from pinvoke.net using the PInvoke.net add-in.
They were modified to use the fully qualified System.Runtime. InteropServices .DllImport reference.
What am I doing wrong?
Comment