[Qt-interest] CLR/.NET/Managed code support in a QT project
Scott Aron Bloom
Scott.Bloom at sabgroup.com
Fri Jul 10 01:50:21 CEST 2009
Further update...
When turning on CLR, for some reason, the QT debugging enhancements are
lost.. Ie, a QString only displays the pointer to the data pointer...
So what I have done, is grouped all my CLR based code into 1 library,
and used the hidden impl structure to hide all the CLR from the
interface..
So I can see QT debugging in native code, I can pass and return QT
objects to the CLR library (Ive tried it with shared and static DLLs)
Inside the library, I cant really debug the QT objects, but I can go
full CLR and debug .NET objects natively.
Scott
-----Original Message-----
From: qt-interest-bounces at trolltech.com
[mailto:qt-interest-bounces at trolltech.com] On Behalf Of Scott Aron Bloom
Sent: Thursday, July 02, 2009 3:19 AM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] CLR/.NET/Managed code support in a QT project
Thanks...
Actually, it wasn't as bad as I thought it would be...
First the good news... You can set /CLR on a portion of your
application libraries...
Ie, if you have 4 libs + a main.cpp, and 1 lib has to be managed, you
can turn on /CLR on the 1 library and your "main.cpp" link library, and
it links up just fine, and runs fine..
My biggest fear, I would be walking into a MD vs MT issue where the
runtime libraries don't line up etc etc... Apparnetly MS has learned :)
Then in the managed portion of code, I wrote from interop functions for
String to QString and vice versa.. Same with array< Byte > and
QByteArray.. Since those are the two I am using the most for now.. and
in reality, the two I will probably ever use :)
Then it was a simple matter of using the .NET calls I needed directly...
Without the /CLR switch.. I would have had to use QAx which in reality..
its really doing for you in the background...
Just some info.. In working with the Amazon Web Services, all the
examples are in C# :(, which frankly sucks for using QT..
For all the http requests, you have to HMAC using SHA256, unfortunately,
the only QT native'esq solution is the QCA from the KDE guys (or its now
with KDE) and it works great for the builtin cryptography routines, but
I was never able to get the plugin based system necessary to get the SHA
256 system to work... (SHA1 is provided without a plugin)
Of course the example code used the .Net class HMACSHA256, in 3 lines
of code you can get your hash
string privateKey = .....;
string textToSign = "textToSign";
HMACSHA256 signer = new HMACSHA256( privateKey );
byte[] sigBytes = signer.ComputeHash( textToSign );
string signature = Convert.ToBase64String( sigBytes );
Well.. with a couple of conversion routines, I got the following code to
work...
QString privateKey = .....;
QString textToSign = "textToSign";
Array<Byte> ^ pKey = QByteArrayToArrayOfByte(privateKey.toUtf8() );
array<Byte> ^ toSign = QByteArrayToArrayOfByte( textToSign.toUtf8() );
HMACSHA256 ^ signer = gcnew HMACSHA256( pKey );
array< Byte > ^ sigBytes = signer->ComputeHash( toSign );
String ^ base64String = Convert::ToBase64String( sigBytes );
QString signature = StringToQString( base64String );
delete toSign;
delete base64String;
delete signature;
delete signer;
Issues I hit... my "signer" really should be a member of the class (the
hash occurs inside a method of a class) . Unfortuantely, you can mix
managed objects inside unmanaged classes, and my class is QObject
based.. and trying to create a managed C++ class that derived from an
unmanaged class was just.. mind boggling :)
Also.. pardon my ignorance... Anyone know how to handle base 64 strings
in QT ? Anyone know? I would prefer to go directly from sigBytes -> a
QString with the proper "QString::from" functionality...
If your interested... here are my conversion routines... Comments are
CERTAINLY welcome!
array< Byte > ^ QByteArrayToArrayOfByte( const QByteArray & byteArray )
{
array< Byte > ^ buffer = gcnew array<Byte>( byteArray.length()
);
for( int ii = 0; ii < byteArray.length(); ++ii )
buffer[ ii ] = byteArray[ ii ];
return buffer;
}
QByteArray ArrayOfByteToQByteArray( array< Byte > ^ byteArray )
{
QByteArray retVal;
for( int ii = 0; ii < byteArray->Length; ++ii )
retVal += byteArray[ ii ];
return retVal;
}
QString StringToQString( String ^ string )
{
QByteArray bArray;
for( int ii = 0; ii < string->Length; ++ii )
bArray += string[ ii ];
return QString::fromUtf8( bArray );
}
String ^ QStringToString( const QString & string )
{
StringBuilder ^ builder = gcnew StringBuilder();
for( int ii = 0; ii < string.length(); ++ii )
{
builder->Append( string[ ii ].toAscii() );
}
String ^ retVal = builder->ToString();
delete builder;
return retVal;
}
Scott
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-
> bounces at trolltech.com] On Behalf Of Volker Hilsheimer
> Sent: Thursday, July 02, 2009 1:50 AM
> To: qt-interest at trolltech.com
> Subject: Re: [Qt-interest] CLR/.NET/Managed code support in a QT
project
>
>
> You can probably bind the .NET assemblies using COM interop, and then
use
> QAxObject to call them.
>
> Otherwise you would probably have to write some managed C++ wrapper...
> yuck.
>
> Volker
>
>
> "Scott Aron Bloom" <Scott.Bloom at sabgroup.com> wrote in message
> news:7DB34253D57D2B47AEB656218FE6AAEF1717E3 at onshorecs.com...
> Before I go down this road, I was wondering if anyone has already done
> so.. I found a couple of "using QT via C#" but not calling C#/.NET
code
> directly using CLR inside a QT project.
>
>
>
> If you have any insights/precautions before I go down this path, I
would
> appricate it.
>
>
>
> Scott
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list