New topic: passing strings to external function
<http://forums.realsoftware.com/viewtopic.php?t=46545> Page 1 of 1 [ 4 posts ] Previous topic | Next topic Author Message louisd Post subject: passing strings to external functionPosted: Sun Jan 13, 2013 10:52 am Joined: Fri Jul 27, 2012 9:52 am Posts: 5 Hello all; I am new to RB, so perhaps this is a very simple issue. I tried to find an answer to my question in the forums and in the documentation, without success. I am using RB 2012R2 on Windows 7 64bits Here is my issue: I am trying to use a print and preview engine that I have used successfully for years with VB6. It is based on a few DLL's. We simply pass data to the engine and it builds the page as we go. it is very convenient. Anyway, I tried a simple "Hello World" program. Once I figured that I need to pass either CString or WString to the external functions, I got rid of communications issues. Now, one issue remains that I don't understand at all: It appears that I am passing only the first character of my strings to the extenal functions. "Hello World" outputs as "H". "C:\Temp\Test.tmp" becomes simply "C" (instead of finding the test file in my temp directory, I find it named "C" in the app directory. The code in the Action event of my Print button is as follows. It is obviously most simplistic: Declare Function FindWindowA Lib "user32.dll" (sClassName As CString, sWinName As WString) As Integer Declare Function dpStartDoc Lib "ddoc32.dll" (ByVal hParent as int32, ByVal zTitle as WString, ByVal zFile as Wstring, ByVal iUOM as int16, ByVal iPaper as int16, ByVal iOrient as int16, ByVal iBin as int16, ByVal iOptions as int16) as int16 Declare Sub dpEndDoc Lib "ddoc32.dll" (ByVal iHandle as int16, ByVal iOptions as int16) Declare Sub dpFont Lib "ddoc32.dll" (ByVal iHandle as int16, ByVal iStyle as int16, ByVal Size as single, ByVal iColor as int32, ByVal zFontName as Wstring ) Declare Sub dpText Lib "ddoc32.dll" (ByVal iHandle as int16, ByVal x as single, ByVal y as single, ByVal iAlign as int16, ByVal zText as Wstring ) Dim LeDocument as int16 dim RetVal as int16 dim F as string dim P as wstring dim T as wstring Dim AppWindowHandle As Int32 AppWindowHandle = FindWindowA (nil,"DDOC_Demo") T= "Print Test" F = specialfolder.Documents.AbsolutePath P= F + "test.tmp" LeDocument =dpStartDoc (AppWindowHandle, T,P, DDOC_INCH, DDOC_PAPER_LETTER, DDOC_PORTRAIT, DDOC_BIN_AUTO, DDOC_ZOOMFIT) retval = msgbox (cstr(ledocument),0) Retval = msgbox (P,0) call dpFont (Ledocument, DDOC_FONTNORMAL, 18, 0, "Calibri" ) 'use Calibri, 18 points, black call dptext (ledocument,4.25, 5.5, DDOC_CENTER, "Hello World" ) 'print Hello World in the middle of the page call dpEndDoc (LeDocument, DDOC_END_VIEW) 'close the temp file and preview the document before sending to a printer In case one wonders, once I have this proof of concept working, I plan to make a class to handle my printing interface. Any help resolving my issue will be greatly appreciated. Regards, Louis Desjardins Top timhare Post subject: Re: passing strings to external functionPosted: Sun Jan 13, 2013 4:57 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 11982 Location: Portland, OR USA Quote: It appears that I am passing only the first character of my strings to the extenal functions. "Hello World" outputs as "H". That would seem to indicate that the dll is expecting a cstring instead of a wstring. The second byte of the wstring will be null, which terminates the string. Top timhare Post subject: Re: passing strings to external functionPosted: Sun Jan 13, 2013 5:02 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 11982 Location: Portland, OR USA 2 other observations: If AppWindowHandle is supposed to be the current window (that the pushbutton is in), you can use Self.Handle directly. You do not need to dim your strings as wstring or cstring. Use normal RB strings in your code and define them as cstring (or wstring) in the declare. RB will convert the string as needed. You should never need to use wstring or cstring directly. Top louisd Post subject: Re: passing strings to external functionPosted: Sun Jan 13, 2013 6:30 pm Joined: Fri Jul 27, 2012 9:52 am Posts: 5 Thank you Timhare; As you probably guessed, I am still at the early stages of the transition from VB6 to RB. Your observations are more than welcome! I made changes as per your suggestions and all is working fine. I will now proceed to creating a class to wrap the use of the multiple functions and subs in the print engine. Best regards, Louis Desjardins Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 4 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
