将二进制数据从Qt / C ++ DLL传递到Delphi主机应用程序 [英] Passing binary data from Qt/C++ DLL into Delphi host application
问题描述
在程序中,我想在Delphi中使用QImage.bits()。所以,在Qt我创建了一个dll。 dll源代码下面列出:
In a Program I want to uses QImage.bits() in Delphi. So, in Qt I was created a dll. the dll Source Code Listed in below:
test.h:
#ifndef TEST_H
#define TEST_H
#include "test_global.h"
extern "C"{
TESTSHARED_EXPORT uchar* testFunc();
}
#endif // TEST_H
test.cpp:
#include "test.h"
#include <QtGui>
QImage image;
uchar* testFunc(){
image.load("c:\\1.png","PNG");
return (uchar*)image.constBits();
}
在Delphi Side中,我使用这个代码来使用Qt dll: p>
and in the Delphi Side I use this code for using Qt dll:
function testFunc(): PByteArray; external 'test.dll';
// ...
procedure TForm3.Button1Click(Sender: TObject);
var
bStream: TBytesStream;
P: PByteArray;
Size: Cardinal;
begin
P := testFunc;
Size := Length(PAnsiChar(P)); // AnsiChar = 1 Byte
bStream := TBytesStream.Create();
try
bStream.Write(P[0], Size); // Works Fine (^_^)
bStream.Position := 0;
bStream.SaveToFile('c:\scr.txt');
finally
bStream.Free;
end;
end;
当我调用dll函数没有任何数据返回!
你能帮我吗?
when I call dll function no any data returned! can you help me?
更新1:
在实际情况下,我的Qt函数非常复杂,在Delphi中写它有很多原因。事实上,原始功能从一个设备捕获一个screenShot,并在主内存中工作。因此我想发送这个图像字节到Delphi为显示它在TImage惠特没有保存在硬盘和类似的记忆。并在本主题中我刚刚创建了一个简单的类似功能,用于简单的调试和可测试性。是否可能通过为这个问题写一个True Simple代码来帮助我?
Update 1: In real situation my Qt function is very complex and I can't write it in Delphi for many reasons. In fact orginal function taked a screenShot from a device and work on It in Main Memory. and as a result I want send this image bytes to Delphi for Show It on a TImage whit no save It on Hard Disk and similar memories. and in this topic I just created a simple similar function for simple debugging and testability. Is It Possible to help me by Writing a True Simple code for this problem? thanx a lot for you. (-_-)
推荐答案
问题解决。 (^ _ ^)
Problem solved. (^_^)
解决方法:
在Qt侧:
test.h:
#ifndef TEST_H
#define TEST_H
#include "test_global.h"
extern "C"{
TESTSHARED_EXPORT char* testFunc(int &a);
}
#endif // TEST_H
test.cpp:
#include "test.h"
#include <QtGui>
QImage image;
QByteArray ba;
char* testFunc(int &a){
image.load("c:\\2.png","PNG");
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer,"PNG");
a = ba.size();
return ba.data();
}
在Delphi Side:
In Delphi Side:
function testFunc(var aByteCount: DWORD): PByte;cdecl external 'test.dll';
// ...
var
bStream: TBytesStream;
Size: DWORD;
procedure TForm3.Button1Click(Sender: TObject);
var
P: PByte;
s: TStringList;
begin
Caption := '0';
P := testFunc(Size);
bStream := TBytesStream.Create();
try
bStream.Write(P[0], Size);
bStream.Position := 0;
bStream.SaveToFile('c:\my.png');
finally
Caption := IntToStr(Size);
end;
end;
Thanx再次为Arioch The和David Heffernan。 (^ _ ^)
Thanx again for "Arioch The" and ""David Heffernan"". (^_^)
这篇关于将二进制数据从Qt / C ++ DLL传递到Delphi主机应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!