PureBasic Prototype definition

Just starting out? Need help? Post your questions and find answers here.
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

PureBasic Prototype definition

Post by wayne-c »

I have the following functions from "WinSCard.dll" and would like to create Prototype in PureBasic. I am not sure which variable types to use (.i, .l, ...) and how the exact Prototype definition should be. I would appreciate if somebody with knowledge could convert. Thank you!

Code: Select all

;internal Static extern int SCardEstablishContext(Int32 dwScope, IntPtr pReserved1, IntPtr pReserved2, out Int32 hContext);
;internal Static extern int SCardListReaders(Int32 hContext, byte[] cardReaderGroups, byte[] readersBuffer, out UInt32 readersBufferLength);
;internal Static extern int SCardGetStatusChange(Int32 hContext, UInt32 timeoutMilliseconds, [In, Out] SmartCardReaderState[] readerStates, Int32 readerCount);

Code: Select all

Int32 --> .i? .l?
IntPtr --> .i?
UInt32 --> ?
As you walk on by, Will you call my name? Or will you walk away?
devox
User
User
Posts: 32
Joined: Thu Apr 01, 2021 7:25 pm

Re: PureBasic Prototype definition

Post by devox »

Looks like the types you've shown here are dotnet types and therefore I don't think you can call from native to dotnet without using c++/cli to wrap them. I think you could also do COM wrappers.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: PureBasic Prototype definition

Post by cas »

This is just a regular old Win32 API.

https://docs.microsoft.com/en-us/window ... ishcontext
https://docs.microsoft.com/en-us/window ... streadersw
https://docs.microsoft.com/en-us/window ... tuschangew

I converted the code example from MSDN but i can not test it because i do not have smart cards on my system. I hope it works for you:

Code: Select all

EnableExplicit

#SCARD_SCOPE_USER = 0
#SCARD_SCOPE_TERMINAL = 1
#SCARD_SCOPE_SYSTEM = 2

#SCARD_AUTOALLOCATE = -1

#SCARD_S_SUCCESS = 0
#SCARD_E_NO_READERS_AVAILABLE = $8010002E
;other error codes: https://docs.microsoft.com/en-us/windows/win32/secauthn/authentication-return-values

Prototype.l proto_SCardEstablishContext(dwScope.l, *pvReserved1, *pvReserved2, *phContext)
Prototype.l proto_SCardReleaseContext(*hContext)
Prototype.l proto_SCardListReadersW(*hContext, *mszGroups, *mszReaders, *pcchReaders)
Prototype.l proto_SCardGetStatusChangeW(*hContext, dwTimeout.l, *rgReaderStates, cReaders.l)
Prototype.l proto_SCardFreeMemory(*hContext, *pvMem)

Define hDLL = OpenLibrary(#PB_Any, "Winscard.dll")
If hDLL
  Define SCardEstablishContext.proto_SCardEstablishContext = GetFunction(hDLL, "SCardEstablishContext")
  Define SCardListReaders.proto_SCardListReadersW = GetFunction(hDLL, "SCardListReadersW")
  Define SCardGetStatusChange.proto_SCardGetStatusChangeW = GetFunction(hDLL, "SCardGetStatusChangeW")
  Define SCardFreeMemory.proto_SCardFreeMemory = GetFunction(hDLL, "SCardFreeMemory")
  Define SCardReleaseContext.proto_SCardReleaseContext = GetFunction(hDLL, "SCardReleaseContext")
  
  If SCardEstablishContext And SCardListReaders And SCardGetStatusChange And SCardFreeMemory And SCardReleaseContext
    Define hSC
    Define lReturn = SCardEstablishContext(#SCARD_SCOPE_USER, #Null, #Null, @hSC)
    If lReturn = #SCARD_S_SUCCESS
      Define *pmszReaders
      Define cch.l = #SCARD_AUTOALLOCATE
      lReturn = SCardListReaders(hSC, #Null, @*pmszReaders, @cch)
      Select lReturn
        Case #SCARD_E_NO_READERS_AVAILABLE
          Debug "Reader is not in groups"
          ;Take appropriate action.
          ;...
        Case #SCARD_S_SUCCESS
          ;Do something with the multi string of readers.
          ;Output the values.
          ;A double-null terminates the list of values.
          Define *pReader.Character = *pmszReaders
          While *pReader\c <> '0'
            ;Display the value.
            Define output.s = PeekS(*pReader)
            Debug "Reader: " + output.s
            ;Advance to the next value.
            *pReader + StringByteLength(output.s) + SizeOf(Character)
          Wend
          ;Free the memory.
          Define lReturn2.l = SCardFreeMemory(hSC, *pmszReaders)
          If lReturn2 <> #SCARD_S_SUCCESS
            Debug "Failed SCardFreeMemory, error code: 0x" + Hex(lReturn2)
          EndIf
        Default
          Debug "Failed SCardListReaders, error code: 0x" + Hex(lReturn)
      EndSelect
      Define lReturn3.l = SCardReleaseContext(hSC)
      If lReturn3 <> #SCARD_S_SUCCESS
        Debug "Failed SCardReleaseContext, error code: 0x" + Hex(lReturn3)
      EndIf
    Else
      Debug "Failed SCardEstablishContext, error code: 0x" + Hex(lReturn)
    EndIf
  EndIf
EndIf
devox
User
User
Posts: 32
Joined: Thu Apr 01, 2021 7:25 pm

Re: PureBasic Prototype definition

Post by devox »

ahh cool. When I saw IntPtr, and Int32 I automatically thought of Dotnet :)
Post Reply