C# read USB Hid UPS status - c#

UPS is Soltec Apa-650 with "megatec battery driver".
Even being in the device manager battery area, it doesn't appear in the Win32_Battery.
Status is available with Upsilon 2000 software (remaining, temperature, etc...).
But now I need to read these values from a C# program
First idea: After seeing the payload with Wireshark, I want to replay it with c#. Any Idea?
For example, I tried pinvoke
deviceHandle = Kernel32.CreateFile(deviceInstancePath, 0, Constants.FileShareRead | Constants.FileShareWrite, IntPtr.Zero, Constants.OpenExisting, 0, 0);
=> The handle is ok
var success = Kernel32.WriteFile( deviceInformation.WriteHandle, outputReportBuffer, outputReportBuffer.Length, ref numberOfBytesWritten, IntPtr.Zero);
=> The success returns false
Does anybody know where I can find the failing explanation?
Any help would be appreciated (sorry for my English, I'm a french guy)
Edit
Thank's to aja here is the answer about the failing explaination
Win32Exception a = new Win32Exception(Marshal.GetLastWin32Error());
Console.WriteLine(a.Message)

Related

C++ Create a handle to a NdisProt driver

I try to write to a NdisProt driver and send raw ethernet packets. I imported some C++ comands to my C# program, so that I can access the driver. When I try to open a handle to the driver I always get a invalid handle. I already tried it with just "NdisProt" as the path, but it didn't solve it. Do you have any suggestions why i get a invalid handle?
private bool OpenDriver()
{
// User the CreateFile API to open a handle to the file
this.m_iHandle = CreateFile("\\\\.\\NdisProt,
GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
// Check to see if we got a valid handle
if ((int)m_iHandle <= 0)
{
// If not, then return false and reset the handle to 0
this.m_iHandle = IntPtr.Zero;
return false;
}
If you now any other solutions to send raw ethernet packets in a C# program please let me know.
Thanks for any help!
Update: I just solved the problem by adding another manifest, so that the application is run as admin.
The solution with the NDIS Driver did still not work so I searched for another solution. I found the SharpPcap library. With that library I am able to modify the packets I want to send e.g. change the Destination-MAC-Adress.
Thanks for your answers!
If you now any other solutions to send raw ethernet packets in a C#
program please let me know.
What about Socket class?
Constructor:
public Socket (System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType),
where socketType can take the next value: SocketType.Raw
If you now any other solutions to send raw ethernet packets in a C# program please let me know.
You can import functions from Windows Winsock library and use SOCK_RAW flag when creating a socket.
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);

HASP HL working demo needed for C#

Okay. Well, I know this question has a good chance of being closed within the first 10 minutes, but I am going to ask it anyways for I have spent almost day and an half trying to find a solution. Still, I can't figure this one out. There is not much info on this on the Internet not even on the HASP (safenet) website although they have demos.
I have a HASP HL USB dongle. I try to convert their demo and test run it but for the life of me I simply can't get it to login even. It keeps raising Aladdin.HASP.HaspStatus.HaspDotNetDllBroken exception.
However, if I run the C version of their demo, it works perfectly.
Here is the Csharp version of my code:
Aladdin.HASP;
HASP myHasp = new HASP();
var thestatus = myHasp.Login(vender_code);
myHasp.Logout;
I would like to login to USB HASP and get its HaspID and the settings in its memory.
Thanks in advance,
It might be that you aren't having all dependencies for the HASP runtime. I'm packing with the app:
hasp_windows_NNNNN.dll (NNNNN = your number)
hasp_net_windows.dll
MSVCR71.DLL (added manually)
msvc runtime 80
One runtime library is required by HASP and it doesn't tell you which one unless you put it in the DEPENDS.EXE utility (you probably have you on your Visual Studio installation).
To log in (and read some bytes):
byte[] key = new byte[16];
HaspFeature feature = HaspFeature.FromFeature(4);
string vendorCode = "your vendor string, get it from your tools";
Hasp hasp = new Hasp(feature);
HaspStatus status = hasp.Login(vendorCode);
if (HaspStatus.StatusOk != status)
{
// no license to run
return false;
}
else
{
// read some memory here
HaspFile mem = hasp.GetFile(HaspFileId.ReadOnly);
mem.Read(key, 0, 16);
status = hasp.Logout();
if (HaspStatus.StatusOk != status)
{
//handle error
}
}
Hope it helps. My HASPed software works like a charm. BTW, wasn't able to put envelope around .NET app under no combination of settings.

php memcached enyim client flag problem

I am trying to use memcached with both a php (memcached) and C# (enyim) client.
I have a scenario where I want to CAS a value in php. To do this I am using the following code:
$memcached = new Memcached;
$memcached->addServer('localhost', 11211) or die ("Could not connect");
$memcached->setOption(Memcached::OPT_COMPRESSION, false); // the enyim client doesn't support compression
do {
$entries = $memcached->get($theKey, null, $cas);
if ($memcached->getResultCode() == Memcached::RES_NOTFOUND) {
$entry = somearray("foo");
$memcached->add($theKey, $entry);
} else {
$entries[] = "bar";
$memcached->cas($cas, $theKey, $entries);
}
}
while ($memcached->getResultCode() != Memcached::RES_SUCCESS AND $memcached->getResultCode() != Memcached::RES_END);
This works all well and fine to begin with.
But then, when the C# client CAS's the same value it goes wrong.
Php gives a warning at:
$entries = $memcached->get($theKey, null, $cas);
namely that:
PHP Warning: Memcached::get(): could not uncompress value in ... at line ...
And as a result an infinite loop occurs.
Now I tried to get the key from the memcached server via telnet and the data was right there.
In php I am also able to SET to this key without a problem.
I noticed one thing: after the php client has SET something, the flag was 0.
Now after the C# client has CAS'd the value, the flag was 274.
Is there some flag collision on the php lib going on? Or is it something else?
If anyone can help me resolve this problem I'd be gratefull!
lordstyx
[EDIT]
Well then. Since this question isn't getting an answer let me put it differently.
Is there a way to stop the C# client from setting flag 274?
So I eventually found an answer to my problem. It might not help all of you, because I switched from the Enyim to the BeIT memcached client (http://code.google.com/p/beitmemcached/)
Now to make the BeIT client compatible with the php client you have to change Serializer.cs
In the enum SerializedType I changed the number of "String" to 0 and ByteArray to 2, which gave this:
internal enum SerializedType : ushort
{
ByteArray = 2,
Object = 1,
String = 0, //mod: turned around the numbers for String and ByteArray so it is compatible with php client
Datetime = 3,
....
I believe I went with BeIT because I couldn't find or figure out how the flags worked in the Enyim client. If you understand how it work though, I'm sure that you can change that client in a same manner
If I am not wrong, mixing languages/platforms with memcached is a bad idea. See another question on similar topic.
For the record. Since the library is open source, then its possible to modify.
In the class Enyim.Caching.Memcached.DefaultTranscoder, change the function
public static uint TypeCodeToFlag(TypeCode code)
{
return (uint)((int)code | 0x0100);
}
to
public static uint TypeCodeToFlag(TypeCode code)
{
if (code == TypeCode.String)
{
return 0;
}
return (uint)((int)code | 0x0100);
}
274 = TypeCode.String | 0x0100
Since i don't know the memcache protocol then i don't know whats doing. However, php's memcache requires 0, so i set to zero when the variable defined is a string.
ps: beitmemcached hasn't be updated for a long while.

.NET or COM HID iCLASS Smart Card Reader

I have coding I almost always use with my Omnikey RFID CardMan 5321 smart cards. Problem is we received new cards today which are marked "HID iCLASS GL" which do not appear to be working well with our coding.
Without going through the whole source, our problem is arising when we are calling the following line, which basically tells us the length of the data:
lResult = SCardTransmit(hCard, 0, bytCommand, lLen, 0, byReadBuffer, iReturnlength)
We are returning only a length of 2, which the data is marked as "x69 x86". Even if I tell it to read all 255 chr's the rest are just marked as null.
Now I know our reader can read these cards since the OMNIKEY Diagnostic tool is showing us the following:
Status: Smart Card Inserted
FW: 5.10
Port: USB
Lib: 1.0
Smart Card Nme: iCLASS 32KS 8x2+16
ART: Valid
Protocol: ISO 15693 (Part 2)
PICCtoPCD: 26,48 kbps
PCDtoPICC: 26,48 kbps
Frequ: 13.56 MHz
As I explained before, everything is working fine in my coding except no data is being returned for my card besides "x69 x86", which is surely not correct.
If anyone has any experience reading from a HID iCLASS card, I would greatly appreciate some feedback on how to. Even if we have to license software, that is ok.
Thanks in advance!
in case you are trying to access physical access data, I would thoroughly check the crypto protocol between reader and host first and also meke sure you are using a reader with teh latest firmware (5.20 for the OMNIKEY 5321).
I would also introduce code to check the card system withour secure communication channel between host and reader application.
Further references:
http://www.hidglobal.com/documents/ok_contactless_developer_guide_an_en.pdf
The reason cause you get a 2 Byte array is cause your command runs on an error so the chip returns only SW1 and SW2 Flag
in your case it's meaning is
x69 --> Command not allowed (further qualification in SW2, see table 17)
x86 --> Command not allowed (no current EF)
So you might proof that your application file on the chip is correctly selected
further information #
http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-4_5_basic_organizations.aspx#table17

How to Remove SIP button of windows mobile

How to remove the SIP button of Windows mobile? I need a solution for Windows Mobile V6.
Please post some sample app, or link about how to remove the SIP button.
I have tried out this technique..
SHFullScreen(this.Handle, SHFS_HIDESIPBUTTON);
This is not working for me. If you know please post full code.
If you only want to remove the SIP, this works for me (I'm using CeGCC as my compiler), and no .dll linking is needed (tested on a HTC Universal running WM6.1).
HWND hWndSipButton = FindWindow(TEXT("MS_SIPBUTTON"), NULL);
if(hWndSipButton != NULL) {
ShowWindow(hWndSipButton, SW_HIDE);
}
Keep in mind you'll also need to insert this code at places right after windows mobile likes to restore the SIP (e.g. put it in response to a WM_ACTIVATE message too).
If you want the entire display surface for your application, then this should do the trick:
iDisplayWidth = GetSystemMetrics(SM_CXSCREEN);
iDisplayHeight = GetSystemMetrics(SM_CYSCREEN);
SHFullScreen(hwndClient, SHFS_HIDESIPBUTTON | SHFS_HIDETASKBAR | SHFS_HIDESTARTICON);
MoveWindow(hwndClient, 0, 0, iDisplayWidth, iDisplayHeight, TRUE);

Categories

Resources