简体   繁体   中英

Migrating to VB.NET: what datatype to use for “hcontext” in an x64 project?

I have a old VB6 project.Now I migrating it in VB.Net on vs2008 and the solution platform now I have to use 64bit.In the old code the variable hContext was declared as Integer.

 Dim hContext As Integer

And used as:

Dim rc As Integer
dwScope = SCARD_SCOPE_USER
rc = SCardEstablishContext(dwScope, 0, 0, hContext)

When I debug the code the hContext create problem. This is due to it define as a Integer(32bit).

Now the problem is "What datatype should I use for hContext"? I have also used different datatype like Long, ULong, IntPtr....

NOTE When I debug the code the hcontext take 4byte address.but in 64bit I take hContext as IntPtr which is platform dependent,But it show only 1byte address. And I am not able to establish the connection.

I suspect the question is "what is the correct signature for SCardEstablishContext in a 64-bit project?"

The C WinAPI signature is as follows:

LONG WINAPI SCardEstablishContext(
  __in   DWORD dwScope,
  __in   LPCVOID pvReserved1,
  __in   LPCVOID pvReserved2,
  __out  LPSCARDCONTEXT phContext
);

Pointer types ("LP...") should be IntPtr and LONG/DWORD types should map to Integer -- this will be correct for a WinAPI call in either a 32-bit or a 64-bit build. (In some cases it is nice to specify a managed structure type instead of IntPtr and let the .NET interoperability/pinvoke automatically marshal everything.)

pinvoke.net is sometimes helpful -- see pinvoke.net: SCardEstablishConnection and *note how the VB.NET signature at top is wrong -- but care needs to be taken because definitions are sometimes incorrect and/or incomplete ;-)

The correct pinvoke signature, for an opaque context value, is:

<DllImport("winscard.dll", SetLastError:=True)>
Public Shared Function SCardEstablishContext(
    dwScope as Integer,
    pvReserved1 as IntPtr,
    pvReserved2 as IntPtr,
    <out>() phContext as IntPtr) As Integer
End Function

Happy coding.

An integer in VB.Net is defined as 32bits, even when running in a 64bit process.

From the MSDN docs :

Holds signed 32-bit (4-byte) integers that range in value from -2,147,483,648 through 2,147,483,647.

Your SCardEstablishContext() function likely calls to unmanaged code that wants to be 32bit. Therefore I would use Integer.

You may also have to specify an x86 (32bit) soluction (rather than Any CPU or x64/64bit) because of this function reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM