How to use .H files in C#?

  • Thread starter Thread starter Mike Yeager
  • Start date Start date
M

Mike Yeager

I'm accessing a windows API call and need to use a constant defined in a ..H file. In the past, it was easy enough to read the .H file and use the value directly in the program, but now I have one defined like this:

#define SCARD_PCI_T0 (&g_rgSCardT0Pci)

Can anyone tell me how to use this in a C# program?

Thanks,
Mike
 
You can't. Create a static constant in a suitable location in your project and assign the same value as is found in the .h file. There are tons of examples of this in OpenNETCF...

Paul T.

I'm accessing a windows API call and need to use a constant defined in a .H file. In the past, it was easy enough to read the .H file and use the value directly in the program, but now I have one defined like this:

#define SCARD_PCI_T0 (&g_rgSCardT0Pci)

Can anyone tell me how to use this in a C# program?

Thanks,
Mike
 
Right, but there ISN'T a static value. That's my problem!

#define SCARD_PCI_T0 (&g_rgSCardT0Pci)

Mike

"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT com> wrote in message You can't. Create a static constant in a suitable location in your project and assign the same value as is found in the .h file. There are tons of examples of this in OpenNETCF...

Paul T.

I'm accessing a windows API call and need to use a constant defined in a .H file. In the past, it was easy enough to read the .H file and use the value directly in the program, but now I have one defined like this:

#define SCARD_PCI_T0 (&g_rgSCardT0Pci)

Can anyone tell me how to use this in a C# program?

Thanks,
Mike
 
You can use unsafe code to take a pointer to an object in managed code, if that's what the API call is expecting. That seems *very* surprising to me, though. It seems more likely that the implementation that you happen to be looking at does that. I don't recognize the #define, but it just appears to me that the API is creating that for convenience. If you built a managed class that duplicated the SCARD_IO_REQUEST functionality, it seems like you could declare a couple of instances of that for g_rgSCardT0Pci, g_rgSCardT1Pci, and g_rgSCardRawPci with no problem. For that matter, I don't see the necessity to even keep those things around as globals at all...

Paul T.

Right, but there ISN'T a static value. That's my problem!

#define SCARD_PCI_T0 (&g_rgSCardT0Pci)

Mike

"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT com> wrote in message You can't. Create a static constant in a suitable location in your project and assign the same value as is found in the .h file. There are tons of examples of this in OpenNETCF...

Paul T.

I'm accessing a windows API call and need to use a constant defined in a .H file. In the past, it was easy enough to read the .H file and use the value directly in the program, but now I have one defined like this:

#define SCARD_PCI_T0 (&g_rgSCardT0Pci)

Can anyone tell me how to use this in a C# program?

Thanks,
Mike
 
You should remove this macro and use suitable replacement instead. What is
suitable depends on context.
In this case this macro returns a pointer to the variable, so you probably
can use a reference.
Change function(s) which takes SCARD_PCI_T0 so it would pass this argument
by reference.

For example:


void foo(int * bar) {
*bar = 1;
...
}

foo(SCARD_PCI_T0);
---------------------------

void foo (ref int bar) {
bar = 1;
...
}

foo (ref g_rgSCardT0Pc);

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "Mike Yeager" <[email protected]>
References: <[email protected]>
Subject: Re: How to use .H files in C#?
Date: Tue, 3 May 2005 14:08:03 -0600
Lines: 109
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_001C_01C54FE9.857181F0"
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: 216.184.30.48.taosnet.com 216.184.30.48
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.compactframework:28272
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

Right, but there ISN'T a static value. That's my problem!
#define SCARD_PCI_T0 (&g_rgSCardT0Pci)
Mike
"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT
com> wrote in message news:[email protected]...
You can't. Create a static constant in a suitable location in your
project and assign the same value as is found in the .h file. There are
tons of examples of this in OpenNETCF...
Paul T.
I'm accessing a windows API call and need to use a constant defined
in a .H file. In the past, it was easy enough to read the .H file and use
the value directly in the program, but now I have one defined like this:
 
Someone posted this code on the Universal Thread which seems to work:

[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(string fileName) ;

[DllImport("kernel32.dll")]
private extern static void FreeLibrary(IntPtr handle) ;

[DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr handle, string
procName);

//Get the address of Pci from "Winscard.dll".
private IntPtr GetPciT0()
{
IntPtr handle = LoadLibrary("Winscard.dll") ;
IntPtr pci = GetProcAddress(handle, "g_rgSCardT0Pci") ;
FreeLibrary(handle) ;
return pci ;
}

Well, it works on Winscard.dll anyway. The .dll I'm using from the hardware manufacturer chokes on it. I guess I'll have to write them and find out what the constants are...

Even the Microsoft implementation defines the constant this way. In fact after over an hour with Google, every hit I found tells you nothing more than what I originally posted. What an ugly design for a parameter that can only be a few values. Would it be that hard to make the docs say something like:

0 = T0
1 = T1
2 = Raw

Instead of:

SCARD_PCI_T0
SCARD_PCI_T1
SCARD_PCI_RAW

Then having a separate (.H) file that defines

#define SCARD_PCI_T0 (&g_rgSCardT0Pci)
#define SCARD_PCI_T0 (&g_rgSCardT1Pci)
#define SCARD_PCI_T0 (&g_rgSCardRawPci)

Then HARD CODING the constant in the .dll that's accepting the paramter in the first place?

I guess that allows compatibility for implementations that can't use 0, 1 and 2 <g>.


Mike

"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT com> wrote in message You can use unsafe code to take a pointer to an object in managed code, if that's what the API call is expecting. That seems *very* surprising to me, though. It seems more likely that the implementation that you happen to be looking at does that. I don't recognize the #define, but it just appears to me that the API is creating that for convenience. If you built a managed class that duplicated the SCARD_IO_REQUEST functionality, it seems like you could declare a couple of instances of that for g_rgSCardT0Pci, g_rgSCardT1Pci, and g_rgSCardRawPci with no problem. For that matter, I don't see the necessity to even keep those things around as globals at all...

Paul T.

Right, but there ISN'T a static value. That's my problem!

#define SCARD_PCI_T0 (&g_rgSCardT0Pci)

Mike

"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT com> wrote in message You can't. Create a static constant in a suitable location in your project and assign the same value as is found in the .h file. There are tons of examples of this in OpenNETCF...

Paul T.

I'm accessing a windows API call and need to use a constant defined in a .H file. In the past, it was easy enough to read the .H file and use the value directly in the program, but now I have one defined like this:

#define SCARD_PCI_T0 (&g_rgSCardT0Pci)

Can anyone tell me how to use this in a C# program?

Thanks,
Mike
 
Back
Top