View Full Version : char* to QString: conversion
abghosh
7th March 2010, 07:05
Hi
While calling dll file from vb.net I am passing a string value to the qt dllas follows:
<DllImport("G:\Varen_CS\02_Projects\01_Softwares\highFreqTrad\ test\debug\test.dll", _
CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function _ZN11QtEngineDll7randIntEv(ByRef u As String) As String
End Function
Now my qtcode goes as follows:
QtEngineDll::QtEngineDll(const char* url)
{
m = url;
}
const char* QtEngineDll::randInt()
{
QByteArray d(m);
QString result = d.data();
QString fileName = QDir::currentPath() + "/Test.txt";
QFile file(fileName);
file.open(QIODevice::Append | QIODevice::Text);
file.write(result + "\n");
return result;
}
But the file output is something like this:
, Abhijit U‰åVSēpÂE¸‰$è T"][07/03/10 - 12:03:46], Abhijit U‰åVSēpÂE¸‰$è T
Can you pls tell me what is the wrong that I am committing in the code?
Thanks in advance.
toutarrive
7th March 2010, 07:57
QByteArray stores raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings.
You can't get Unicode character whit it.
toutarrive
7th March 2010, 08:04
Use instead QString::fromUtf8 ( const char * str, int size = -1 ) .
Read the Qt doc.
abghosh
7th March 2010, 08:59
Thanks toutarrive.
However if I use the following:
const char* test = "Abhijit";
QByteArray d(test);
result = QString::number(d.size());
QString fileName = QDir::currentPath() + "/Test.txt";
QFile file(fileName);
file.open(QIODevice::Append | QIODevice::Text);
file.write(QString(test) + "\n");
return result;
everything is going fine. But if instead of defining test in the program itself, if I pass test value as an parameter from vb.net then some absurd result is thrown. Can anybody put some light on this?
Thanks in advance.
Lesiok
7th March 2010, 13:31
Can You guarantee that in QtEngineDll::randInt() pointer delivered to QtEngineDll constructor is steel point to this same data ?
Maybe it will be better to create QByteArray object in QtEngineDll constructor.
abghosh
7th March 2010, 14:58
Hi Lisiok
Can u pls demonstrate the same in this context.
Any way thnks for suggesting a better method.
toutarrive
7th March 2010, 17:47
I don't see in your code where you are handling the unicode characters.
abghosh
8th March 2010, 02:40
Hi
The problem as far as I have found is in passing the parameter may it be string or integer. The argument value is not at all passed properly. Even if I passed some integer value it is taking it as 1 and not the value passed actually.
Cant understand whether it is a problem in my vb.net call or the dll function definition.
I have attached the files required for building the DLL file in Qt 4. Note that the entry point name is taken from the dll file reader.
If I have committed some mistake in making the DLL file can you pls modify it.
Thanks in advance.
ChrisW67
8th March 2010, 06:51
I'm not familiar with mating .Net code to straight C++ code, but it seems that the parameter list here:
Private Shared Function _ZN11QtEngineDll7randIntEv(ByRef u As String) As String
(one parameter) doesn't match the one here:
const char* QtEngineDll::randInt() (no parameter, but probably requires an implied this pointer)
Any value passed by VB.Net in the u parameter has nowhere to go in the C++ (except perhaps being misinterpreted as a this pointer).
Other things to ponder: Your randInt() tries to dump a string set in the constructor to a file, and not a parameter passed to the method. Where is your object constructed? How is VB.Net setting a "this" pointer when it calls the randInt() method?
abghosh
8th March 2010, 09:32
Hi ChrisW67
The function mentioned by you is the older one. I have recently attached the new file where I just want to pass some integer value.
Passing integer value is also having error.
What I now think is to pass the parameter using some pointer. But I have no idea about reading pointer variables in Qt. Can you pls put some light on the same.
Thanks in advance.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.