seit ein paar Tagen beschäftige ich mich mal wieder etwas intensiver mit PureBasic. Ich bin ja schon eine weile hier im Forum. Viele Themen habe ich noch nicht geschrieben. Aber ich lese viel mit. Insofern ist es schon mehrfach ein Thema gewesen, die Stringrückgabe aus einer DLL.
Alle Quellen dazu findet Ihr auch hier: http://em.q-soft.ch/files/get/kWRg449VWg/pbdlltest.zip.
(Visual Studio 2008 SP1 und PureBasic 4.40 x86)
Ich habe folgendes angefangen. Als Idee diente mir http://www.purebasic.fr/english/viewtop ... =5&t=28544.
Die Quellcodes habe ich leicht modifiziert.
PBdll.pb
Code: Alles auswählen
; http://www.purebasic.fr/english/viewtopic.php?f=5&t=28544
;
EnableExplicit
ProcedureDLL.l IsPowerOf2(x.l)
Protected t.l=0
If (x&(x-1))=0
t=1
EndIf
ProcedureReturn t
EndProcedure
ProcedureDLL.l IsPowerOf(power.l, base.l)
Protected basePow.l
Protected count.l
If base < 0 Or power < 0
ProcedureReturn -1
EndIf
If base = 0
If power = 1
ProcedureReturn 0
Else
ProcedureReturn 1
EndIf
EndIf
If base = 1
If power = 1
ProcedureReturn 1
Else
ProcedureReturn -1
EndIf
EndIf
If power = 1
ProcedureReturn 0
EndIf
basePow = base
While $7FFFFFFF / basePow > 1
count+1
If basePow = power
ProcedureReturn count
EndIf
basePow * base
Wend
ProcedureReturn -1
EndProcedure
Global ReturnString.s = ""
ProcedureDLL.s Round2n(n.f,places.l)
If places < 0 : places = 0 : EndIf
Protected r.f = 0.5 * Pow(10,-1*places) : Protected T.f = Pow(10,places)
ReturnString = StrF(Int((n+r)*T)/T,places)
ProcedureReturn ReturnString
EndProcedure
Debug IsPowerOf2(33)
Debug IsPowerOf(49,7)
Debug Round2n(3.1415927,4)
Code: Alles auswählen
EnableExplicit
; Import-File created by Lib2PBImport
; Libname: PBdll.lib
; created: 2009/12/16 12:25
Import "PBdll.lib"
IsPowerOf2.l(a.l) As "_IsPowerOf2@4"
IsPowerOf.l(a.l,b.l) As "_IsPowerOf@8"
Round2n(a.l,b.l) As "_Round2n@8"
EndImport
Define a.s =""
Define ReturnString1.s =""
Define ReturnString2.s =""
Debug "IsPowerOf2(33) = " + Str(IsPowerOf2(33))
Debug "IsPowerOf(49,7) = " + Str(IsPowerOf(49,7))
Debug "Round2n(3.1415927, 4) = " + Str(Round2n(3.1415927,4))
a.s = PeekS(Round2n(3.1415927, 4))
Debug a
; Versuch mit CallFunction
If OpenLibrary(0, "PBdll.dll")
ReturnString1.s = PeekS(CallFunction(0,"Round2n",3.1415927, 4))
EndIf
Debug "ReturnString1 = " + ReturnString1
Debug "Islibrary = " + Str(IsLibrary(0))
CloseLibrary(0)
; Versuch mit Prototype
Prototype.s protoRound2n(n.f, places.l)
If OpenLibrary(0, "PBdll.dll")
Define rstring.protoRound2n = GetFunction(0, "Round2n")
ReturnString2.s = rstring(3.1415927, 4)
EndIf
Debug "ReturnString1 = " + ReturnString1
Debug "ReturnString2 = " + ReturnString2
Debug "Islibrary = " + Str(IsLibrary(0))
CloseLibrary(0)
Als Ausgabe der Testbed erhalte ich:
Round2n(3.1415927, 4) hat als Ergebnis 0.0000. Es sollte aber 3.1416 sein.IsPowerOf2(33) = 0
IsPowerOf(49,7) = 2
Round2n(3.1415927, 4) = 10093976
0.0000
ReturnString1 = 0.0000
Islibrary = 9505168
ReturnString1 = 0.0000
ReturnString2 =
Islibrary = 9505168
Wenn ich die Dll aus einem C/C++ Programm verwende ist das Ergebnis richtig.
Auch hier mein Testbed:
Code: Alles auswählen
// pbdlltest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#pragma comment(lib, "PBdll.lib")
extern "C" __declspec(dllimport) int __stdcall IsPowerOf2(int n);
extern "C" __declspec(dllimport) int __stdcall IsPowerOf(int power, int base);
extern "C" __declspec(dllimport) char* __stdcall Round2n(float n,int places);
int _tmain(int argc, _TCHAR* argv[])
{
char ch;
int n,m;
n = IsPowerOf2(33);
m = IsPowerOf(49, 7);
printf("%d\n", n);
printf("%d\n", m);
printf("%s\n", Round2n((float) 3.1415927,4));
printf("%s\n","press return to end");
ch = getchar();
return 0;
}
Ich habe hier schon im Forum einmal geschaut, insbesondere0
2
3.1416
press return to end
http://forums.purebasic.com/german/vi ... light=dll
und andere.
Es wäre schön, wenn Ihr mir auf den richtigen weg helft.
Besten Dank!
Liebe Grüße,
Frank