2016-09-26 4 views
-1

をwchar_tにLPSTRから変換できません:は、私たちは条件を使用してウェブ上でのBluetoothの例からコードの一部を得た

ULONG NameToBthAddr(_In_ const LPWSTR pszRemoteName, _Out_ PSOCKADDR_BTH pRemoteBtAddr) 
{ 
    INT    iResult = CXN_SUCCESS; 
    BOOL   bContinueLookup = FALSE, bRemoteDeviceFound = FALSE; 
    ULONG   ulFlags = 0, ulPQSSize = sizeof(WSAQUERYSET); 
    HANDLE   hLookup = NULL; 
    PWSAQUERYSET pWSAQuerySet = NULL; 

    ZeroMemory(pRemoteBtAddr, sizeof(*pRemoteBtAddr)); 

    pWSAQuerySet = (PWSAQUERYSET)HeapAlloc(GetProcessHeap(), 
     HEAP_ZERO_MEMORY, 
     ulPQSSize); 
    if (NULL == 
     ) { 
     iResult = STATUS_NO_MEMORY; 
     wprintf(L"!ERROR! | Unable to allocate memory for WSAQUERYSET\n"); 
    } 

    // 
    // Search for the device with the correct name 
    // 
    if (CXN_SUCCESS == iResult) { 

     for (INT iRetryCount = 0; 
      !bRemoteDeviceFound && (iRetryCount < CXN_MAX_INQUIRY_RETRY); 
      iRetryCount++) { 
      // 
      // WSALookupService is used for both service search and device inquiry 
      // LUP_CONTAINERS is the flag which signals that we're doing a device inquiry. 
      // 
      ulFlags = LUP_CONTAINERS; 

      // 
      // Friendly device name (if available) will be returned in lpszServiceInstanceName 
      // 
      ulFlags |= LUP_RETURN_NAME; 

      // 
      // BTH_ADDR will be returned in lpcsaBuffer member of WSAQUERYSET 
      // 
      ulFlags |= LUP_RETURN_ADDR; 

      if (0 == iRetryCount) { 
       wprintf(L"*INFO* | Inquiring device from cache...\n"); 
      } 
      else { 
       // 
       // Flush the device cache for all inquiries, except for the first inquiry 
       // 
       // By setting LUP_FLUSHCACHE flag, we're asking the lookup service to do 
       // a fresh lookup instead of pulling the information from device cache. 
       // 
       ulFlags |= LUP_FLUSHCACHE; 

       // 
       // Pause for some time before all the inquiries after the first inquiry 
       // 
       // Remote Name requests will arrive after device inquiry has 
       // completed. Without a window to receive IN_RANGE notifications, 
       // we don't have a direct mechanism to determine when remote 
       // name requests have completed. 
       // 
       wprintf(L"*INFO* | Unable to find device. Waiting for %d seconds before re-inquiry...\n", CXN_DELAY_NEXT_INQUIRY); 
       Sleep(CXN_DELAY_NEXT_INQUIRY * 1000); 

       wprintf(L"*INFO* | Inquiring device ...\n"); 
      } 

      // 
      // Start the lookup service 
      // 
      iResult = CXN_SUCCESS; 
      hLookup = 0; 
      bContinueLookup = FALSE; 
      ZeroMemory(pWSAQuerySet, ulPQSSize); 
      pWSAQuerySet->dwNameSpace = NS_BTH; 
      pWSAQuerySet->dwSize = sizeof(WSAQUERYSET); 
      iResult = WSALookupServiceBegin(pWSAQuerySet, ulFlags, &hLookup); 

      // 
      // Even if we have an error, we want to continue until we 
      // reach the CXN_MAX_INQUIRY_RETRY 
      // 
      if ((NO_ERROR == iResult) && (NULL != hLookup)) { 
       bContinueLookup = TRUE; 
      } 
      else if (0 < iRetryCount) { 
       wprintf(L"=CRITICAL= | WSALookupServiceBegin() failed with error code %d, WSAGetLastError = %d\n", iResult, WSAGetLastError()); 
       break; 
      } 

      while (bContinueLookup) { 
       // 
       // Get information about next bluetooth device 
       // 
       // Note you may pass the same WSAQUERYSET from LookupBegin 
       // as long as you don't need to modify any of the pointer 
       // members of the structure, etc. 
       // 
       // ZeroMemory(pWSAQuerySet, ulPQSSize); 
       // pWSAQuerySet->dwNameSpace = NS_BTH; 
       // pWSAQuerySet->dwSize = sizeof(WSAQUERYSET); 
       if (NO_ERROR == WSALookupServiceNext(hLookup, 
        ulFlags, 
        &ulPQSSize, 
        pWSAQuerySet)) { 

        // 
        // Compare the name to see if this is the device we are looking for. 
        // 

     if ((pWSAQuerySet->lpszServiceInstanceName != NULL) && 
     (CXN_SUCCESS == _wcsicmp_l(pWSAQuerySet->lpszServiceInstanceName, pszRemoteName))) { 
    } 
    } 

、lpszServiceInstancenameは私が見winSock2.h内で定義されています。

#ifdef UNICODE 
typedef WSAQUERYSETW WSAQUERYSET; 
typedef PWSAQUERYSETW PWSAQUERYSET; 
typedef LPWSAQUERYSETW LPWSAQUERYSET; 
typedef WSAQUERYSET2W WSAQUERYSET2; 
typedef PWSAQUERYSET2W PWSAQUERYSET2; 
typedef LPWSAQUERYSET2W LPWSAQUERYSET2; 
#else 
typedef WSAQUERYSETA WSAQUERYSET; 
typedef PWSAQUERYSETA PWSAQUERYSET; 
typedef LPWSAQUERYSETA LPWSAQUERYSET; 
typedef WSAQUERYSET2A WSAQUERYSET2; 
typedef PWSAQUERYSET2A PWSAQUERYSET2; 
typedef LPWSAQUERYSET2A LPWSAQUERYSET2; 
#endif /* UNICODE */ 




typedef struct _WSAQuerySetA 
    { 
     DWORD   dwSize; 
     LPSTR   lpszServiceInstanceName; 
     LPGUID   lpServiceClassId; 
     LPWSAVERSION lpVersion; 
     LPSTR   lpszComment; 
     DWORD   dwNameSpace; 
     LPGUID   lpNSProviderId; 
     LPSTR   lpszContext; 
     DWORD   dwNumberOfProtocols; 
     __field_ecount(dwNumberOfProtocols) LPAFPROTOCOLS lpafpProtocols; 
     LPSTR   lpszQueryString; 
     DWORD   dwNumberOfCsAddrs; 
     __field_ecount(dwNumberOfCsAddrs) LPCSADDR_INFO lpcsaBuffer; 
     DWORD   dwOutputFlags; 
     LPBLOB   lpBlob; 
    } WSAQUERYSETA, *PWSAQUERYSETA, *LPWSAQUERYSETA; 
    typedef __struct_bcount(dwSize) struct _WSAQuerySetW 
    { 
     DWORD   dwSize; 
     LPWSTR   lpszServiceInstanceName; 
     LPGUID   lpServiceClassId; 
     LPWSAVERSION lpVersion; 
     LPWSTR   lpszComment; 
     DWORD   dwNameSpace; 
     LPGUID   lpNSProviderId; 
     LPWSTR   lpszContext; 
     DWORD   dwNumberOfProtocols; 
     __field_ecount(dwNumberOfProtocols) LPAFPROTOCOLS lpafpProtocols; 
     LPWSTR   lpszQueryString; 
     DWORD   dwNumberOfCsAddrs; 
     __field_ecount(dwNumberOfCsAddrs) LPCSADDR_INFO lpcsaBuffer; 
     DWORD   dwOutputFlags; 
     LPBLOB   lpBlob; 
    } WSAQUERYSETW, *PWSAQUERYSETW, *LPWSAQUERYSETW; 

私はエラーを取得するしかし:私はしませんので

_wcsicmp' : cannot convert parameter 1 from 'LPSTR' to 'const wchar_t *'

これは明らかに動作しません。ユニコードを使用しますが、マルチバイト文字セットを使用します。あなたはリンゴとリンゴを比較するためにpWSAQuerySet->lpszServiceInstanceNamewcharに変換するために何をお勧めしますか?

グーグルでエラーが発生しても、私はそれほど助けにはなりませんでしたが、私はそれがうまく理解できないことに恐れています。 int MultiByteToWideChar()を正しく使用する手助けができますか?前もって感謝します!

+1

となし?それは信じがたいようです。 –

答えて

0

_wcsicmp_l()は2つの文字列を必要としますが、最初のパラメータにchar文字列を渡しているため、エラーになります。つまり、pWSAQuerySetWSAQUERYSETAを指し、WSAQUERYSETWを指していません。コードがTCHARベースのWSAQUERYSETを使用しているため、プロジェクトはUnicodeではなくMBCSに設定されています。

pszRemoteNamewchar_t文字列の場合は、char文字列と比較することはできません。これらは完全に異なるデータ型です。そのうちの1つをもう一方に変換する必要があります。この場合、あなたは、あなたが例えば、pszRemoteNameに変換された値を比較することができ、MultiByteToWideChar()またはequivilentを使用してwchar_tlpszServiceInstanceName値を変換する必要があります

int len = MultiByteToWideChar(CP_ACP, 0, pWSAQuerySet->lpszServiceInstanceName, -1, NULL, 0); 
if (len > 0) { 
    wchar_t *pszServiceInstanceName = new wchar_t[len]; 
    MultiByteToWideChar(CP_ACP, 0, pWSAQuerySet->lpszServiceInstanceName, -1, pszServiceInstanceName, len); 
    if (CXN_SUCCESS == _wcsicmp_l(pszServiceInstanceName, pszRemoteName) { 
     //... 
    } 
    delete[] pszServiceInstanceName; 
} 

代替がそうWSAQUERYSETWの代わりWSAQUERYSETAを使用するようにコードを変更することですlpszServiceInstanceNameフィールドはcharの代わりにwchar_tを使用しています。 Unicode APIを使用するために、プロジェクト全体をUnicodeに変更する必要はありません。 TCHARベースのAPIの使用をやめ、そのままprintf()の代わりにwprintf()を使用しているように、Unicode APIを直接使用してください。この場合、直接WSALookupServiceBeginW()WSALookupServiceNextW()を使用して、例えば:助けたこの質問のタイトルをグーグルとき1が見つかったものの

BOOL NameToBthAddr(_In_ LPCWSTR pszRemoteName, _Out_ PSOCKADDR_BTH pRemoteBtAddr) 
{ 
    INT    iResult; 
    BOOL   bContinueLookup = TRUE, bRemoteDeviceFound = FALSE; 
    ULONG   ulFlags = 0, ulPQSSize = sizeof(WSAQUERYSETW); 
    HANDLE   hLookup = NULL; 
    PWSAQUERYSETW pWSAQuerySet = NULL; 

    ZeroMemory(pRemoteBtAddr, sizeof(*pRemoteBtAddr)); 

    pWSAQuerySet = (PWSAQUERYSETW) HeapAlloc(GetProcessHeap(), 0, ulPQSSize); 
    if (!pWSAQuerySet) { 
     wprintf(L"!ERROR! | Unable to allocate memory for WSAQUERYSET\n"); 
     return FALSE; 
    } 

    // 
    // Search for the device with the correct name 
    // 

    for (int iRetryCount = 0; (!bRemoteDeviceFound) && (iRetryCount < CXN_MAX_INQUIRY_RETRY) && (bContinueLookup); ++iRetryCount) { 
     // 
     // WSALookupService is used for both service search and device inquiry 
     // LUP_CONTAINERS is the flag which signals that we're doing a device inquiry. 
     // 
     ulFlags = LUP_CONTAINERS; 

     // 
     // Friendly device name (if available) will be returned in lpszServiceInstanceName 
     // 
     ulFlags |= LUP_RETURN_NAME; 

     // 
     // BTH_ADDR will be returned in lpcsaBuffer member of WSAQUERYSET 
     // 
     ulFlags |= LUP_RETURN_ADDR; 

     if (0 == iRetryCount) { 
      wprintf(L"*INFO* | Inquiring device from cache...\n"); 
     } 
     else { 
      // 
      // Flush the device cache for all inquiries, except for the first inquiry 
      // 
      // By setting LUP_FLUSHCACHE flag, we're asking the lookup service to do 
      // a fresh lookup instead of pulling the information from device cache. 
      // 
      ulFlags |= LUP_FLUSHCACHE; 

      // 
      // Pause for some time before all the inquiries after the first inquiry 
      // 
      // Remote Name requests will arrive after device inquiry has 
      // completed. Without a window to receive IN_RANGE notifications, 
      // we don't have a direct mechanism to determine when remote 
      // name requests have completed. 
      // 
      wprintf(L"*INFO* | Unable to find device. Waiting for %d seconds before re-inquiry...\n", CXN_DELAY_NEXT_INQUIRY); 
      Sleep(CXN_DELAY_NEXT_INQUIRY * 1000); 

      wprintf(L"*INFO* | Inquiring device ...\n"); 
     } 

     // 
     // Start the lookup service 
     // 
     hLookup = NULL; 
     ZeroMemory(pWSAQuerySet, ulPQSSize); 
     pWSAQuerySet->dwNameSpace = NS_BTH; 
     pWSAQuerySet->dwSize = ulPQSSize; 

     iResult = WSALookupServiceBeginW(pWSAQuerySet, ulFlags, &hLookup); 
     if (SOCKET_ERROR == iResult) { 
      wprintf(L"=CRITICAL= | WSALookupServiceBegin() failed with error code %d\n", WSAGetLastError()); 
      break; 
     } 

     do { 
      // 
      // Get information about next bluetooth device 
      // 
      // Note you may pass the same WSAQUERYSET from LookupBegin 
      // as long as you don't need to modify any of the pointer 
      // members of the structure, etc. 
      // 
      // ZeroMemory(pWSAQuerySet, ulPQSSize); 
      // pWSAQuerySet->dwNameSpace = NS_BTH; 
      // pWSAQuerySet->dwSize = ulPQSSize; 

      do { 
       iResult = WSALookupServiceNextW(hLookup, ulFlags, &ulPQSSize, pWSAQuerySet); 
       if (SOCKET_ERROR != iResult) { 
        break; 
       } 

       iResult = WSAGetLastError(); 
       if (WSAEFAULT != iResult) { 
        break; 
       } 

       PWSAQUERYSETW pNewWSAQuerySet = (PWSAQUERYSETW) HeapReAlloc(GetProcessHeap(), 0, pWSAQuerySet, ulPQSSize); 
       if (!pNewWSAQuerySet) { 
        wprintf(L"!ERROR! | Unable to re-allocate memory for WSAQUERYSET\n"); 
        iResult = WSA_NOT_ENOUGH_MEMORY; 
        break; 
       } 

       pWSAQuerySet = pNewWSAQuerySet; 
      } 
      while (true); 

      if (NO_ERROR != iResult) { 
       if (WSA_E_NO_MORE != iResult) { 
        bContinueLookup = FALSE; 
       } 
       break; 
      } 

      // 
      // Compare the name to see if this is the device we are looking for. 
      // 

      if ((pWSAQuerySet->lpszServiceInstanceName) && (0 == _wcsicmp_l(pWSAQuerySet->lpszServiceInstanceName, pszRemoteName))) { 
       bRemoteDeviceFound = TRUE; 
       CopyMemory(pRemoteBtAddr, pWSAQuerySet->lpcsaBuffer->RemoteAddr.lpSockaddr, sizeof(*pRemoteBtAddr)); 
       break; 
      } 
     } 
     while (true); 

     WSALookupServiceEnd(hLookup); 
    } 

    HeapFree(GetProcessHeap(), 0, pWSAQuerySet); 

    return bRemoteDeviceFound; 
} 
+0

ご返信ありがとうございます。私はこれに慣れておらず、経験もありません。あなたは詳細を教えていただけますか?問題は、次のように定義されているpWSAQuerySetが何か分かりません。 PWSAQUERYSET pWSAQuerySet = NULL; \t ZeroMemory(pRemoteBtAddr、sizeof(* pRemoteBtAddr)); \t pWSAQuerySet =(PWSAQUERYSET)ます。HeapAlloc(GetProcessHeap()、 \t \t HEAP_ZERO_MEMORY、 \t \t ulPQSSize)。 そして、私は(CXN_SUCCESS == _stricmp_l(pWSAQuerySet-> lpszServiceInstanceName、pszRemoteName))しようとした場合pszRemoteNameは、このようにconstのLPWSTR です)残念ながら私はUnicodeに切り替えることができません のいずれかを助けにはなりません。 – user6812514

+0

ご協力いただきありがとうございます。上記の質問を編集しました。 – user6812514

+0

@ user6812514あなたは重要な細部を省いた:「もの」は何を正確にするのか?あなたはヒープ上に 'WSAQUERYSET'を割り当てています(なぜ、' new'の代わりに 'HeapAlloc()を使用していますか?そして割り当てられたメモリを解放するコードはどこですか?)。 WSAQUERYSETはデータを持っています。これは、省略したコードを変更する必要があるため重要です。 [最小、**完全**、および検証可能な例](http://stackoverflow.com/help/mcve)を入力してください。 –

関連する問題