Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinSCard.SCardTransmit: Cannot marshal 'parameter #2': Generic types cannot be marshaled. #404

Closed
franciscosamuel opened this issue Jun 1, 2023 · 0 comments

Comments

@franciscosamuel
Copy link

I'm trying to invoke SCardTransmit with the code below and getting an exception with the message "Cannot marshal 'parameter #2': Generic types cannot be marshaled.".

Looking at how other functions that also take a SafeCoTaskMemStruct are declared, it seems like the problem is exactly what the exception message says: the parameters have to be declared as IntPtr

public bool SendCommand(byte[] data, out byte[] output)
		{
			var sendPci = new WinSCard.SCARD_IO_REQUEST();
			sendPci.dwProtocol = _activeProtocol;
			sendPci.cbPciLength = (uint)Marshal.SizeOf(sendPci);

			var recvPci = new WinSCard.SCARD_IO_REQUEST();
			recvPci.dwProtocol = _activeProtocol;
			recvPci.cbPciLength = (uint)Marshal.SizeOf(recvPci);

			uint recvLength = 256;
			output = new byte[recvLength];

			GCHandle dataPinned = GCHandle.Alloc(data, GCHandleType.Pinned);
			GCHandle recvDataPinned = GCHandle.Alloc(output, GCHandleType.Pinned);

			var _lastRet = WinSCard.SCardTransmit(_card,
											 new SafeCoTaskMemStruct<WinSCard.SCARD_IO_REQUEST>(sendPci),
											 dataPinned.AddrOfPinnedObject(),
											 (uint)data.Length,
											 new SafeCoTaskMemStruct<WinSCard.SCARD_IO_REQUEST>(recvPci),
											 recvDataPinned.AddrOfPinnedObject(),
											 ref recvLength);

			dataPinned.Free();
			recvDataPinned.Free();

			return _lastRet.Succeeded;
		}
@dahall dahall closed this as completed in a925f43 Jun 2, 2023
dahall added a commit that referenced this issue Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant