Question

Configuration:
J'ai une DLL COM qui appelle une méthode dans un DLL géré C #. Cette fonction retourne une chaîne C # tableau [], qui est à un SAFEARRAY marshalée.

Problème:
Lorsque je tente d'accéder aux chaînes dans le safearray je ne reçois que le premier caractère de la chaîne. Qu'est-ce que je fais mal?

Le code:

    // Pointer to the managed interface
    DatabasePtr pODB(__uuidof(DBClass));

    // Get the string[] array from the managed method
    SAFEARRAY* safearray = pODB->GetStringArray();

    HRESULT hresult;

    long ubound;
    long lbound;

    hresult = SafeArrayGetUBound(safearray, 1, &ubound);
    hresult = SafeArrayGetLBound(safearray, 1, &lbound);

    long index;
    BSTR fromarray;

    for (; lbound <= ubound; lbound++)
    {
        index = lbound;

        hresult = SafeArrayGetElement(safearray, &index, (void*)&fromarray);

        char buffer[512];
        sprintf_s(buffer,"%s",fromarray);

        MessageBox(0, (LPCSTR)buffer, "...", 0);
    }

Merci pour votre aide,
-Sean!

Était-ce utile?

La solution

Le BSTR est une chaîne de caractères Unicode, vous devez donc utiliser un tampon de wchar_t et wsprintf_s. En ce moment, u imprimer la partie ANSI du premier caractère unicode puis arrêtez sur le \ 0. Et s'il vous plaît, s'il vous plaît, ne pas empiler trop-plein comme ça (sic!). Utilisez le _vsnwprintf_s_l sûr et sa famille, votre code est le plus grand plaisir d'un pirate informatique comme il est en ce moment et u'll être pwned. Voir http://msdn.microsoft.com/en- nous / bibliothèque / d3xd30zz (VS.80) .aspx

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top