Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Declare DLL call caused an exception (!!!)
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00506373
Message ID:
00506401
Vues:
19
This message has been marked as the solution to the initial question of the thread.
>
>FUNCTION SYSGetUserName
>LOCAL lcUserName, lnBufSize, RetCode, NullCharPos
>
>    lcUserName = REPLICATE(CHR(32), 80)
>    lnBufSize = 80
>
>    *---call WINAPI
>    Declare Integer GetUserName IN WIN32API String @lpBuffer, Integer nSize
>    RetCode = GetUserName(@lcUserName, lnBufSize)
>    CLEAR DLLS
>
>    *---search for the end of the string
>    NullCharPos = AT(CHR(0), lcUserName)
>    IF NullCharPos > 0 THEN
>        lcUserName = LEFT(lcUserName, NullCharPos - 1)
>    ELSE
>       lcUserName = ""
>    ENDIF
>
>    RETURN lcUserName
>
>ENDFUNC
>
>>
>>Try adding the @ sign infront of lcUserName in the call to GetUserName, so it is called "By Reference".
>>
>>Btw. There is a command for everything and I'm still finding new stuff every week. Instead of REPLICATE(CHR(32), 80), you could do a SPACE(80)
>>
>>Hope that helped,
>
>Thanks for the quick answer!
>
>I tried that (adding the @) but it didn't work (same error message.) Plus, I knew about the SPACE function but prefered using REPLICATE because, at the beginning, I was filling the string with null characters (chr(0)).
>
>What next?
>
Stephane,

The problem is, as Sergey pointed out, that the integer parameter must be passed by reference. Here's the C++ declaration
BOOL GetUserName(
  LPTSTR lpBuffer,  // name buffer
  LPDWORD nSize     // size of name buffer
);
The "LP" prefix means "Long Pointer", indicating passing by refernce. This is the reason your attempt "blew up". It's passed by reference because it receives the number of bytes copied to the buffer. Thus, it'll allow to simplify your code. Note that if the function fails (returns 0) because the buffer is too small, the nSize parameter will contain the size required.
George

Ubi caritas et amor, deus ibi est
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform