How do you write "IntPtr.Zero" in PB?

Just starting out? Need help? Post your questions and find answers here.
sec
Enthusiast
Enthusiast
Posts: 790
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

How do you write "IntPtr.Zero" in PB?

Post by sec »

Thanks :?

Just want convert this small function in the C code to the PB,

Code: Select all

public static int GetPCSCReaders(out List<string> smartCardReaders, out string errMsg)
{
    errMsg = string.Empty;
    smartCardReaders = new List<string>();
    IntPtr hContext;

    try
    {
        int ret = SCardEstablishContext(SCARD_SCOPE_USER, IntPtr.Zero, IntPtr.Zero, out hContext);
        if (ret != SCARD_S_SUCCESS)
        {
            errMsg = "WinSCard GetPCSCReader: EstablishContext Error: " + ret.ToString();
            return ret;
        }

        byte[] readersList = null;
        uint byteCnt = 0;
        ret = SCardListReaders(hContext, null, null, ref byteCnt);
        if (ret != SCARD_S_SUCCESS)
        {
            errMsg = "WinSCard GetPCSCReader: ListReaders Error: " + ret.ToString();
            return ret;
        }

        readersList = new byte[byteCnt];
        ret = SCardListReaders(hContext, null, readersList, ref byteCnt);
        if (ret != SCARD_S_SUCCESS)
        {
            errMsg = "WinSCard GetPCSCReader: ListReaders Error: " + ret.ToString();
            return ret;
        }

        int indx = 0;
        string readerName = string.Empty;
        int i = 0;

        while (readersList[indx] != 0)
        {
            while (readersList[indx] != 0)
            {
                readerName = readerName + (char)readersList[indx++];
            }

            smartCardReaders.Add(readerName);
            i++;

            readerName = "";
            indx++;
        }

    }
    catch (Exception ex)
    {
        errMsg = ex.Message;
    }
    finally
    {

    }
    return 0;
}
P/S: success conversion will post in tip/trick anw :mrgreen:
User avatar
spikey
Enthusiast
Enthusiast
Posts: 586
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: How do you write "IntPtr.Zero" in PB?

Post by spikey »

Not sure why they felt the need to do it that way, particularly as 'null' is used in later calls, but

Code: Select all

#Null
should be fine.
sec
Enthusiast
Enthusiast
Posts: 790
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: How do you write "IntPtr.Zero" in PB?

Post by sec »

Hi, @spikey I tried write in PB, but fails, always get ret2,ret3,ret4 == 6 means "invalid handle". Any idea to fix?
C: (ref: https://www.pinvoke.net/default.aspx/wi ... istreaders )

Code: Select all

long ret = 0;
int hContext;
Int32 pcchReaders = 0;

//establish context
ret = SCardEstablishContext(SCARD_SCOPE_USER,IntPtr.Zero,IntPtr.Zero,out hContext);

//get readers buffer len
ret = SCardListReaders(hContext,IntPtr.Zero,null,ref pcchReaders);
byte[] mszReaders = new byte[pcchReaders];

// fill readers' buffer
ret = SCardListReaders(hContext,IntPtr.Zero ,mszReaders, ref pcchReaders);

ret = SCardReleaseContext(hContext);



in PB:

Code: Select all

Enumeration
  #SCARD_SCOPE_USER            ;    0x0000/* Scope in user space */
  #SCARD_SCOPE_TERMINAL        ;    0x0001/* Scope in terminal */
  #SCARD_SCOPE_SYSTEM          ;    0x0002/* Scope in system */
EndEnumeration 


If OpenLibrary(0, "WinSCard.dll")
  SCardEstablishContext=GetFunction(0,"SCardEstablishContext")
  SCardListReaders=GetFunction(0,"SCardListReadersA")
  SCardReleaseContext=GetFunction(0,"SCardReleaseContext")
  
  hContext.l
  
  ret1 = CallFunctionFast(SCardEstablishContext, #SCARD_SCOPE_USER , #Null, #Null, @hContext)
  debug ret1  
  
  
  pcchReaders.l
  ret2 = CallFunctionFast(SCardListReaders, @hContext, #Null, #Null, @pcchReaders)
  debug ret2 ; invalid handle
  
  mszReaders.s = Space(200)
  ret3 = CallFunctionFast(SCardListReaders, @hContext, #Null, @mszReaders, @pcchReaders)
  debug ret3 ; invalid handle
  
  ret4 = CallFunctionFast(SCardReleaseContext,@hContext)
  debug ret4 ; invalid handle
  
  MessageRequester(peeks(@mszReaders,20),str(ret1) + "-ret2:"+ str(ret2)+"-ret3:"+str(ret3)+"-ret4:"+str(ret4))
  CloseLibrary(0)
endif

User avatar
spikey
Enthusiast
Enthusiast
Posts: 586
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: How do you write "IntPtr.Zero" in PB?

Post by spikey »

Are you using using a 64-bit version of Windows? If so, your .L (32 bit) handles and pointers need to be .I (64 bit) type (hContext and pcchReaders).
sec
Enthusiast
Enthusiast
Posts: 790
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: How do you write "IntPtr.Zero" in PB?

Post by sec »

spikey wrote:Are you using using a 64-bit version of Windows? If so, your .L (32 bit) handles and pointers need to be .I (64 bit) type (hContext and pcchReaders).
I am using 32-bit.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How do you write "IntPtr.Zero" in PB?

Post by Mijikai »

User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: How do you write "IntPtr.Zero" in PB?

Post by Derren »

try getting rid of the @ here
ret2 = CallFunctionFast(SCardListReaders, @hContext, #Null, #Null, @pcchReaders)
I don't see in MSDN that it wants a pointer to the context handle, but rather the handle itself.
sec
Enthusiast
Enthusiast
Posts: 790
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: How do you write "IntPtr.Zero" in PB?

Post by sec »

Derren wrote:try getting rid of the @ here
ret2 = CallFunctionFast(SCardListReaders, @hContext, #Null, #Null, @pcchReaders)
I don't see in MSDN that it wants a pointer to the context handle, but rather the handle itself.
Mijikai wrote:This might be helpful: http://forums.purebasic.com/english/vie ... 13&t=10093

Code: Select all

Enumeration
  #SCARD_SCOPE_USER            ;    0x0000/* Scope in user space */
  #SCARD_SCOPE_TERMINAL        ;    0x0001/* Scope in terminal */
  #SCARD_SCOPE_SYSTEM          ;    0x0002/* Scope in system */
EndEnumeration 


If OpenLibrary(0, "WinSCard.dll")
  SCardEstablishContext=GetFunction(0,"SCardEstablishContext")
  SCardListReaders=GetFunction(0,"SCardListReadersA")
  SCardReleaseContext=GetFunction(0,"SCardReleaseContext")
  
  hContext.l
  ret1 = CallFunctionFast(SCardEstablishContext, #SCARD_SCOPE_USER , #Null, #Null, @hContext)
  debug ret1  
  
  pcchReaders.l
  ret2 = CallFunctionFast(SCardListReaders, hContext, #Null, #Null, @pcchReaders)
  debug ret2
  
  mszReaders.s = Space(200)
  ret3 = CallFunctionFast(SCardListReaders, hContext, #Null, @mszReaders, @pcchReaders)
  debug ret3
  
  ret4 = CallFunctionFast(SCardReleaseContext,hContext)
  debug ret4
  
  MessageRequester(mszReaders,mszReaders+ str(ret1) + "-ret2:"+ str(ret2)+"-ret3:"+str(ret3)+"-ret4:"+str(ret4))
  CloseLibrary(0)
endif
Hi,
Abit better now: ret1,ret2,ret3,ret4 == 0 , but mszReaders looks junky chars! Any idea to fix?

IMG: https://www.dropbox.com/s/lo0f2ydyp2ir5 ... e.PNG?dl=0
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: How do you write "IntPtr.Zero" in PB?

Post by Bisonte »

This looks like a ASCII/Unicode problem.

Try the functions from the dll with an ending "W", not the ending "A"...

Since PB 5.50 PB is unicode only.
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
sec
Enthusiast
Enthusiast
Posts: 790
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: How do you write "IntPtr.Zero" in PB?

Post by sec »

Bisonte wrote:This looks like a ASCII/Unicode problem.

Try the functions from the dll with an ending "W", not the ending "A"...

Since PB 5.50 PB is unicode only.
Hi
Changed 'A' to 'W':

Code: Select all

Enumeration
  #SCARD_SCOPE_USER            ;    0x0000/* Scope in user space */
  #SCARD_SCOPE_TERMINAL        ;    0x0001/* Scope in terminal */
  #SCARD_SCOPE_SYSTEM          ;    0x0002/* Scope in system */
EndEnumeration 


If OpenLibrary(0, "WinSCard.dll")
  SCardEstablishContext=GetFunction(0,"SCardEstablishContext")
  SCardListReaders=GetFunction(0,"SCardListReadersW")
  SCardReleaseContext=GetFunction(0,"SCardReleaseContext")
  
  hContext.l
  ret1 = CallFunctionFast(SCardEstablishContext, #SCARD_SCOPE_USER , #Null, #Null, @hContext)
  debug ret1  
  
  pcchReaders.l
  ret2 = CallFunctionFast(SCardListReaders, hContext, #Null, #Null, @pcchReaders)
  debug ret2
  
  mszReaders.s = Space(200)
  ret3 = CallFunctionFast(SCardListReaders, hContext, #Null, @mszReaders, @pcchReaders)
  debug ret3
  
  ret4 = CallFunctionFast(SCardReleaseContext,hContext)
  debug ret4

  MessageRequester(mszReaders,mszReaders+ str(ret1) + "-ret2:"+ str(ret2)+"-ret3:"+str(ret3)+"-ret4:"+str(ret4))
  CloseLibrary(0)
endif
Now its displayed, IMG: https://www.dropbox.com/s/hb789wewq5no4 ... 1.PNG?dl=0
Seems missing chars (unicode?). Any idea to fix?

Thanks
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: How do you write "IntPtr.Zero" in PB?

Post by Derren »

Idk, seems to work fine on my PC:
---------------------------
Broadcom Corp Contacted SmartCard 0
---------------------------
Broadcom Corp Contacted SmartCard 00-ret2:0-ret3:0-ret4:0
---------------------------
OK
---------------------------
But it doesn't list my second external card reader
Post Reply