ATL (BSTR) to VB
-
I have a DLL that when I use it in VBS it works fine everytime...but when I use VB, it works the first time, but the second call returns TRASH. Can anyone explain why?? Here is a code snippet: STDMETHODIMP CSetShare::get_pNetDiskEnum(BSTR sServer, BSTR *pVal) { // NetserverDiskEnum Function to // Enumerate list of remote Server Drive Letters. const int ENTRY_SIZE = 3; // Drive letter, colon, NULL LPTSTR pBuf = NULL; DWORD dwLevel = 0; // level must be zero DWORD dwPrefMaxLen = -1; DWORD dwEntriesRead = 0; DWORD dwTotalEntries = 0; NET_API_STATUS nStatus; CComBSTR cstr1; // // Call the NetServerDiskEnum function. // nStatus = NetServerDiskEnum((LPSTR)sServer, dwLevel, (LPBYTE *) &pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries, NULL); // // If the call succeeds, // if (nStatus == NERR_Success) { LPTSTR pTmpBuf; if ((pTmpBuf = pBuf) != NULL) { DWORD i; DWORD dwTotalCount = 0; // // Loop through the entries. // for (i = 0; i < dwEntriesRead; i++) { assert(pTmpBuf != NULL); if (pTmpBuf == NULL) { // On a remote computer, only members of the // Administrators or the Account Operators // local group can execute NetServerDiskEnum. // break; } // // Print drive letter for each drive; // the number of entries actually enumerated; and // the total number of entries available. // cstr1 += pTmpBuf; pTmpBuf += ENTRY_SIZE; dwTotalCount++; } cstr1 += pTmpBuf; } } else cstr1 = "System Error: No Such Machine"; if (pBuf != NULL) // Free the allocated buffer. NetApiBufferFree(pBuf); *pVal = (BSTR)cstr1; // Conversion to BSTR Type for VB return S_OK; } Thanks in advance, Dan
-
I have a DLL that when I use it in VBS it works fine everytime...but when I use VB, it works the first time, but the second call returns TRASH. Can anyone explain why?? Here is a code snippet: STDMETHODIMP CSetShare::get_pNetDiskEnum(BSTR sServer, BSTR *pVal) { // NetserverDiskEnum Function to // Enumerate list of remote Server Drive Letters. const int ENTRY_SIZE = 3; // Drive letter, colon, NULL LPTSTR pBuf = NULL; DWORD dwLevel = 0; // level must be zero DWORD dwPrefMaxLen = -1; DWORD dwEntriesRead = 0; DWORD dwTotalEntries = 0; NET_API_STATUS nStatus; CComBSTR cstr1; // // Call the NetServerDiskEnum function. // nStatus = NetServerDiskEnum((LPSTR)sServer, dwLevel, (LPBYTE *) &pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries, NULL); // // If the call succeeds, // if (nStatus == NERR_Success) { LPTSTR pTmpBuf; if ((pTmpBuf = pBuf) != NULL) { DWORD i; DWORD dwTotalCount = 0; // // Loop through the entries. // for (i = 0; i < dwEntriesRead; i++) { assert(pTmpBuf != NULL); if (pTmpBuf == NULL) { // On a remote computer, only members of the // Administrators or the Account Operators // local group can execute NetServerDiskEnum. // break; } // // Print drive letter for each drive; // the number of entries actually enumerated; and // the total number of entries available. // cstr1 += pTmpBuf; pTmpBuf += ENTRY_SIZE; dwTotalCount++; } cstr1 += pTmpBuf; } } else cstr1 = "System Error: No Such Machine"; if (pBuf != NULL) // Free the allocated buffer. NetApiBufferFree(pBuf); *pVal = (BSTR)cstr1; // Conversion to BSTR Type for VB return S_OK; } Thanks in advance, Dan
*pVal = (BSTR)cstr1; There's your problem right there.
cstr1
is a local variable. Change it to*pVal = cstr1.Detach();
--Mike-- http://home.inreach.com/mdunn/ Push the button, Frank.
-
*pVal = (BSTR)cstr1; There's your problem right there.
cstr1
is a local variable. Change it to*pVal = cstr1.Detach();
--Mike-- http://home.inreach.com/mdunn/ Push the button, Frank.
Thanks Michal, it is bed time in this part of the world...I will test this first thing in the morning, and thanks for the quick response! Thanks in advance, Dan