Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Jun 29, 2017 3:59 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Newbie question: Call API and return string with REALBuildSt
PostPosted: Tue Jan 17, 2012 4:14 am 
Offline

Joined: Thu Nov 08, 2007 4:59 am
Posts: 392
Location: Germany
Hello,

I'm trying to build a simple plugin that calls the WINAPI function "GetWindowsDirectory"
The call itself works - but I can not return the resultig string using REALBuildString()


static REALstring getwindowsdir (void)
{
   TCHAR sResultstring[32767];
   DWORD bufCharCount = 0;

   // Get the Windows directory.
bufCharCount = GetWindowsDirectory( sResultstring, 32767 ) ;

   // Display result
   //_tprintf( TEXT("\nWindows Directory: %s"), sResultstring ); // OK
   //wprintf ( TEXT("\nWindows Directory: %s"), sResultstring ); // OK
&nbsp; &nbsp;//std::wcout << "\nWinDir=" << sResultstring; //OK

&nbsp; &nbsp;// build return string - but how ? I understand that I need to convert TCHAR sResultstring to something that REALBuildString accepts... ?
?? REALstring result = REALBuildString( sResultstring, sResultstring.length );
&nbsp; &nbsp;
&nbsp; &nbsp;return result
}

I use Windows / VC++ 2010


Heinz


Top
 Profile  
Reply with quote  
 Post subject: Re: Newbie question: Call API and return string with REALBui
PostPosted: Tue Jan 17, 2012 9:06 pm 
Offline
User avatar

Joined: Sat May 27, 2006 3:27 pm
Posts: 480
I don't know much about writing plugins. Here is how you get the Windows directory with API converted from VB to RB.

RB
Sub GetWindowsDir() As String
#If TargetWin32
Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA"(ByVal lpBuffer As Ptr, nSize As Integer) As Integer
Dim windir As MemoryBlock
Dim slength As Integer
windir = New MemoryBlock(255)
slength = GetWindowsDirectory( windir , 255 )
Return windir.CString(0)
#EndIf
End Sub


VB
Declare Function GetWindowsDirectory Lib "kernel32.dll" _
Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long

lpBuffer
String variable that receives the path.
nSize
The length in characters of lpBuffer.
Code

the location of the Windows directory
Dim windir As String ' receives path of Windows directory
Dim slength As Long ' receives length of the string returned

windir = Space(255) ' initialize buffer to receive the string
slength = GetWindowsDirectory(windir, 255) ' read the path of the Windows directory
windir = Left(windir, slength) ' extract the returned string from the buffer
Debug.Print "The Windows directory is at: "; windir


Top
 Profile  
Reply with quote  
 Post subject: Re: Newbie question: Call API and return string with REALBui
PostPosted: Wed Jan 18, 2012 5:40 am 
Offline
User avatar

Joined: Wed May 10, 2006 2:42 pm
Posts: 2985
Location: Germany
Not sure why you both waste your time to replicate specialfolder.windows function.


REALBuildString needs a pointer to data and length in bytes, so why not pass sResultstring and bufCharCount*sizeof(TCHAR)?

Greetings
Christian

_________________
See you in Orlando, Florida for Real World 2013
More details and registration here:
http://www.realsoftware.com/community/realworld.php


Top
 Profile  
Reply with quote  
 Post subject: Re: Newbie question: Call API and return string with REALBui
PostPosted: Wed Jan 18, 2012 8:07 am 
Offline

Joined: Thu Nov 08, 2007 4:59 am
Posts: 392
Location: Germany
MonkeybreadSoftware wrote:
Not sure why you both waste your time to replicate specialfolder.windows function.

well, I know there is no practical use for the code - but I try to understand how to build a plugin.

Quote:
REALBuildString needs a pointer to data and length in bytes, so why not pass sResultstring and bufCharCount*sizeof(TCHAR)?

OK... so I've tried this :
REALstring result = REALBuildString( sResultstring, bufCharCount*sizeof(TCHAR ));

But the compiler told me:
error C2665: 'REALBuildString' : none of the 2 overloads could convert all the argument types
c:\plugin tests\app5\app5\app5\rb_plugin.h(128): could be 'REALstring REALBuildString(const char *,int)'
while trying to match the argument list '(TCHAR [32767], DWORD)'

So I need some kind of converting TCHAT->CHAR ... after some more testing I've found this:
void TCharToChar(const wchar_t* in, char* out, int Size)
{
WideCharToMultiByte(CP_ACP, 0, in, wcslen(in)+1, out , Size, NULL, NULL);
}

Now it works :-)

But I get this warning :
C4995: 'REALRegisterMethod': name was marked as #pragma deprecated

Is this somethnig to be worried about ?


Top
 Profile  
Reply with quote  
 Post subject: Re: Newbie question: Call API and return string with REALBui
PostPosted: Wed Jan 18, 2012 8:52 am 
Offline
User avatar

Joined: Wed May 10, 2006 2:42 pm
Posts: 2985
Location: Germany
Well, yes you can convert from unicode chars to normal ansi chars and return that text.

For passtring pointer to REALBuildString, you may need to cast to char*.

And I would not worry about REALRegisterMethod deprecation. If you like, build a module.

Greetings
Christian

_________________
See you in Orlando, Florida for Real World 2013
More details and registration here:
http://www.realsoftware.com/community/realworld.php


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group