Click here to Skip to main content
15,920,602 members
Home / Discussions / C#
   

C#

 
GeneralRe: Masked Textbox Control Pin
Sameer Khan3-Jun-03 13:41
Sameer Khan3-Jun-03 13:41 
GeneralRe: Masked Textbox Control Pin
sajecw5-Jun-03 8:25
sajecw5-Jun-03 8:25 
QuestionAnimation appears to studder? How do you set a frame rate? Pin
EvilDingo3-Jun-03 7:50
EvilDingo3-Jun-03 7:50 
AnswerRe: Animation appears to studder? How do you set a frame rate? Pin
dazinith3-Jun-03 8:52
dazinith3-Jun-03 8:52 
GeneralCrystal Reports & C# Pin
Gaurika Wijeratne3-Jun-03 7:12
Gaurika Wijeratne3-Jun-03 7:12 
GeneralRe: Crystal Reports & C# Pin
Mike Osbahr3-Jun-03 7:25
Mike Osbahr3-Jun-03 7:25 
Generaldraw a chart in an app and drag & drop under Excel Pin
zed3-Jun-03 6:30
zed3-Jun-03 6:30 
GeneralReturn ByRef array from Win32 back to .NET / AllocCoTaskMem Pin
TobyEvans3-Jun-03 5:47
TobyEvans3-Jun-03 5:47 
Hi there,

I’m using a Win32 API dll, Winscard.dll, for which there is no current equivalent in the .NET Framework.

Several of the functions require the use of ByRef arrays, e.g. pointers, when calling the api function. From following the examples in MSDN, I am able to set up the pointers needed for the array, but am having difficulty setting the correct size of the pointer using AllocCoTaskMem.

The function I’m trying to call in Winscard.dll is SCardStatus. From the documentation, this is declared as:

LONG SCardStatus(
SCARDHANDLE hCard,
LPTSTR szReaderName,
LPDWORD pcchReaderLen,
LPDWORD pdwState,
LPDWORD pdwProtocol,
LPBYTE pbAtr,
LPDWORD pcbAtrLen
);

My function declaration in C# is thus:
[DllImport("winscard.dll")]
static unsafe extern int SCardStatus (IntPtr hContext, out StringBuilder szReaderName , ref IntPtr pcchReaderLen, out IntPtr pdwState, out IntPtr pdwProtocol, ref IntPtr pbAtr, ref int pcbAtrLen);

This uses an IntPtr “pbAtr” to receive the array.

I declare an array (emptyATRArray) to act as the original array:
private byte[] emptyATRArray = new byte[32];

And create another variable, “size”, based upon the size of this
int size = emptyATRArray.Length;

Using the template in MSDN as an example, I then create a pointer called “buffer” to pass to the function:
IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(size) * emptyATRArray.Length);

I then call the function:
lngResult = cSmartCard.SCardStatus(lngCard, out strGetReader,ref intReaderNameLen,out intReaderState, out intReaderProtocol, ref buffer, ref retATRLen);

I am happy that the function has worked correctly, as the value of the pcbAtrLen variable, (retATRLen) has correctly changed from 0 (in this case to 13, the expected number of bytes for the particular smartcard), as well as the other ByRef integers.

To extract the values of the updated array, I create a new byte array called returnedATRArray of the size retuned to retATRLen (13), and copy all the values from the buffer to the this new array:
Marshal.Copy(buffer, returnedATRArray,0,retATRLen);

And free the memory:
Marshal.FreeCoTaskMem(buffer);

This then gives me a correctly sized array, which I can cycle through to extract the values:
strATR= new StringBuilder();
foreach (byte atrByte in returnedATRArray) {
strATR.Append (Convert.ToString(atrByte,16) + ' ');
}
Console.Write("ATR: " + strATR.ToString());


However, from cross-checking with another program, I know that the values returned to returnedATRArray are not correct. By fiddling with the parameters to AllocCoTaskMem, I can get different values out of the final array, so I assume that the amount of memory allocated here is crucial.

I’ve been unable to find a clear guide as to how to define the size of the block of memory for such operations. Could anyone help

Cheers

Toby

GeneralSerial port access Pin
Phil J Pearson3-Jun-03 5:08
Phil J Pearson3-Jun-03 5:08 
GeneralRe: Serial port access Pin
3-Jun-03 5:24
suss3-Jun-03 5:24 
GeneralRe: Serial port access Pin
Phil J Pearson3-Jun-03 5:40
Phil J Pearson3-Jun-03 5:40 
GeneralTrace and Config file problem Pin
Le centriste3-Jun-03 4:25
Le centriste3-Jun-03 4:25 
GeneralRe: Trace and Config file problem Pin
James T. Johnson3-Jun-03 10:52
James T. Johnson3-Jun-03 10:52 
GeneralRe: Trace and Config file problem Pin
Le centriste4-Jun-03 3:30
Le centriste4-Jun-03 3:30 
Generalproblem with combo box !!! Pin
kokain3-Jun-03 3:09
kokain3-Jun-03 3:09 
GeneralRe: problem with combo box !!! Pin
apferreira3-Jun-03 3:21
apferreira3-Jun-03 3:21 
GeneralRe: problem with combo box !!! Pin
kokain3-Jun-03 6:27
kokain3-Jun-03 6:27 
GeneralRe: problem with combo box !!! Pin
James T. Johnson3-Jun-03 10:55
James T. Johnson3-Jun-03 10:55 
GeneralRe: problem with combo box !!! Pin
Anonymous6-Jun-03 4:25
Anonymous6-Jun-03 4:25 
GeneralApplyFont in Cristal Report Pin
Givi78394763543-Jun-03 2:56
sussGivi78394763543-Jun-03 2:56 
GeneralALT Key Pin
Valeria Bogdevich3-Jun-03 2:22
Valeria Bogdevich3-Jun-03 2:22 
GeneralRe: ALT Key Pin
Vasudevan Deepak Kumar3-Jun-03 5:00
Vasudevan Deepak Kumar3-Jun-03 5:00 
GeneralRe: ALT Key Pin
Vasudevan Deepak Kumar3-Jun-03 18:40
Vasudevan Deepak Kumar3-Jun-03 18:40 
GeneralRe: ALT Key Pin
3-Jun-03 22:16
suss3-Jun-03 22:16 
GeneralWindows to *nix with Mono Pin
Kannan Kalyanaraman3-Jun-03 0:31
Kannan Kalyanaraman3-Jun-03 0:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.