News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

StringCb... and similar windows functions

Started by traphunter, November 30, 2013, 01:14:10 AM

Previous topic - Next topic

traphunter

GoAsm; Easy Code IDE; ECGo header

Hello everybody!

I want to use the StringCB... functions (e.g. StringCbLengthW) defined in Strsave.h, but the linker quits with:

Error!
The following symbol was not defined in the object file or files:-
StringCbLengthW

My question is, it is necessary to download the whole PSDK to get the Strsave.lib or is there another solution? E.g. linking against a specific *.dll?

Thank you
traphunter

jj2007

I'm not sure if I ever installed the SDK on this machine here, but the libraries are there.
Have you checked C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\strsafe.lib ?

Besides, Masm32 has \Masm32\include\ntstrsafe.inc, but my puter says "The program can't start because ntstrsafe.dll is missing" ::)

Apart from that: Using lstrcpyn etc with some caution is the better choice. These "safe" functions are very slow...

traphunter

yes, I've checked my machine, I didn't have the libraries. I would like to use these functions in a none time critical task, where I didn't have the control over the input string.

jj2007

Quote from: traphunter on November 30, 2013, 02:11:26 AM... where I didn't have the control over the input string.

Safe and fast:

include \masm32\include\masm32rt.inc

.data
dest db 50 dup(?)
afterdest db "Don't trash me please...", 0

.code
somestring db "Hello, this is a simple string intended for testing string algos. It has 100 characters without zero", 0

.code
start:
mov esi, offset somestring
mov edi, offset dest
.if len(esi)>=sizeof dest
mov eax, sizeof dest
.endif
invoke lstrcpyn, edi, esi, eax
MsgBox 0, edi, offset afterdest, MB_OK
exit
end start


;)

traphunter

yes, thank you, but I often try to use existing resources. Really, I would like to know, how to link these windows functions without downloading the whole SDK for getting the strsave.lib.

dedndave

that's a strange one   :redface:

according to msdn, it should be in coredll.dll, and is supported xp sp2 or better
i also found references on the web to ntstrsafe.dll and strsafe.dll

i am running xp sp3, and i don't find any of those dll's on my machine - lol

Pelle's C has coredll.lib  <-------------------------  this one is under WinCE
masm32 has ntstrsafe.inc and ntstrsafe.lib

dedndave


dedndave

http://www.masmforum.com/board/index.php?topic=18010.msg151770#msg151770

quote from Edgar....

QuoteThe string safe functions are not exported by any library, they are inline code in the SDK headers (Strsafe.h) so they would need to be rewritten from scratch for MASM. However in Windows 8 I noticed that a few of them are available in kernel32.dll as exports.

dedndave

http://www.masmforum.com/board/index.php?topic=18010.msg151826#msg151826

i agree with the last 3 posts in that thread from Hutch, Jochen, and Greg
string-safe functions are a "compiler thing"
in assembly language, the program should be designed so that buffer over-flows don't occur   :t
i.e., if you need a string-safe function, you're doing it wrong - lol

traphunter

Ah, thank you very much, in strsave.h are c++ inline functions. I won't use the strsave.lib anymore. ;)

BTW: The question is not whether I need a function, the question is, it's better to reuse or to write code by yourself? The answer: it varies ;)

with best regards
traphunter

dedndave

in assembly language, we might simply create a temp buffer on the stack
that way, you know the buffer is large enough to accomodate the string
thus, the function is never required

i hope that's a little clearer

traphunter

If the stringlength is unknown and unpredictable due to streaminput, I can't locate enough memory anywhere, whether what coding language is used. Now I will use the lstr... functions with own coded wrapper.

I only wanted to know a way to use these StringC... functions. I've searched for that, here in this forum and elsewhere, but maybe with wrong phrases. Due to your hints I know much more than before. Thank you very much! :)

MichaelW

Judging from it not containing a DLL name but containing at least one object module name, the strsafe.lib from my 2003 PSDK is apparently a static library.

;==============================================================================
include \masm32\include\masm32rt.inc
includelib strsafe.lib
;==============================================================================
StringCbLengthA proto :DWORD,:DWORD,:DWORD
;  _In_   LPCTSTR psz,
;  _In_   size_t cbMax,
;  _Out_  size_t *pcb
;==============================================================================
.data
    slength  dd 0
    string   db "my other brother darryl",0
.code
;==============================================================================
;==============================================================================
start:
;==============================================================================
    invoke StringCbLengthA, ADDR string, 24, ADDR slength
    printf("%d\t%d\n\n",eax,slength)
    inkey
    exit
;==============================================================================
end start


0       23


And from strsafe.h:

#define S_OK  ((HRESULT)0x00000000L)

Well Microsoft, here's another nice mess you've gotten us into.

jj2007

I get Unresolved external symbol '_StringCbLengthA@12'. from my C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\strsafe.lib of 30.09.2009...

As Dave wrote, it's a 'compiler thing' - here is its definition from strsafe.h:
STRSAFEAPI
StringCbCopyNA(
    __out_bcount(cbDest) STRSAFE_LPSTR pszDest,
    __in size_t cbDest,
    __in_bcount(cbToCopy) STRSAFE_PCNZCH pszSrc,
    __in size_t cbToCopy)
{
    HRESULT hr;
    size_t cchDest = cbDest / sizeof(char);

    hr = StringValidateDestA(pszDest, cchDest, STRSAFE_MAX_CCH);
   
    if (SUCCEEDED(hr))
    {
        size_t cchToCopy = cbToCopy / sizeof(char);

        if (cchToCopy > STRSAFE_MAX_LENGTH)
        {
            hr = STRSAFE_E_INVALID_PARAMETER;
           
            *pszDest = '\0';
        }
        else
        {
            hr = StringCopyWorkerA(pszDest,
                                   cchDest,
                                   NULL,
                                   pszSrc,
                                   cchToCopy);
        }
    }

    return hr;
}


By the way, we discussed all that already one year ago.

dedndave

ahhhh, Michael - the PSDK has the static library   :t
don't know how i missed that one (i probably searched for ntstrsafe.lib)

the LIB in the masm32 package is undoubtedly an import library - with no DLL to import from   :P