Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15475

Stuck with this Language Detection Service code

$
0
0
Hi,

I am experimenting with this code which uses the native Elscore.lib (Windows 7 and later versions)

The following C code excerpt comes from here :
https://docs.microsoft.com/en-us/win...xt-recognition

Baically, the code is supposed to detect the language a string is written in.

C Code:
Code:

#include <windows.h>
#include <stdio.h>
#include <elscore.h>
#include <elssrvc.h>

#define USER_TEXT ( \
    L"Skip This is a simple sentence. " \
    L"\x0422\x0445\x0438\x0441 \x0438\x0441 \x0415\x043d\x0433\x043b\x0438\x0441\x0445.")
#define USER_TEXT_SKIP (5)

int __cdecl main();
HRESULT CallMappingRecognizeText(PMAPPING_SERVICE_INFO pService);
void PrintAllResults(PMAPPING_PROPERTY_BAG pBag);

int __cdecl main()
{
    MAPPING_ENUM_OPTIONS    EnumOptions;
    PMAPPING_SERVICE_INFO  prgServices = NULL;
    DWORD                  dwServicesCount = 0;
    HRESULT                hResult;

    ZeroMemory(&EnumOptions, sizeof (MAPPING_ENUM_OPTIONS));
    EnumOptions.Size = sizeof (MAPPING_ENUM_OPTIONS);
    // Using the Language Auto-Detection GUID to enumerate LAD only:
    EnumOptions.pGuid = (GUID *)&ELS_GUID_LANGUAGE_DETECTION;
    hResult = MappingGetServices(&EnumOptions, &prgServices, &dwServicesCount);

    if (SUCCEEDED(hResult))
    {
        hResult = CallMappingRecognizeText(&prgServices[0]);
        if (SUCCEEDED(hResult))
        {
            printf("Calling the service %ws has succeeded!\n",
                prgServices[0].pszDescription);
        }
        else
        {
            printf("Calling the service %ws has failed, failure = 0x%x!\n",
                prgServices[0].pszDescription, hResult);
        }
        MappingFreeServices(prgServices);
    }

    return 0;
}

HRESULT CallMappingRecognizeText(PMAPPING_SERVICE_INFO pService)
{
    MAPPING_PROPERTY_BAG bag;
    HRESULT hResult;

    ZeroMemory(&bag, sizeof (MAPPING_PROPERTY_BAG));
    bag.Size = sizeof (MAPPING_PROPERTY_BAG);

    // MappingRecognizeText's dwIndex parameter specifies the first
    // index inside the text from where the recognition should start.
    // We pass USER_TEXT_SKIP, thus skipping the "Skip " part
    // of the input string.
    // Calling without MAPPING_OPTIONS:
    hResult = MappingRecognizeText(pService, USER_TEXT, wcslen(USER_TEXT), USER_TEXT_SKIP, NULL, &bag);
    if (SUCCEEDED(hResult))
    {
        printf("Results from service: %ws\n", pService->pszDescription);
        PrintAllResults(&bag);
        hResult = MappingFreePropertyBag(&bag);
    }
    return hResult;
}

void PrintAllResults(PMAPPING_PROPERTY_BAG pBag)
{
    WCHAR * p;

    // The return format of the Language Auto-Detection is a
    // double null-terminated registry-formatted array of strings.
    // Every string of the array is null-terminated and there's an
    // empty string specifying the end of the array.
    for (p = (WCHAR *)pBag->prgResultRanges[0].pData; *p; p += wcslen(p) + 1)
    {
        printf("%ws\n", p);
    }
}



Now, here is my attempt so far in translating the above C code but the code fails at the call of the MappingRecognizeText API function. I get the error code : -2147024809 (Parameter is incorrect)

The calls to MappingGetServices and MappingFreeServices both seem to work fine and I get a count of services of (9).

Note: as opposed to the above C code, I am passing here a (0) in the first argument of the MappingGetServices API (pOptions) in order to retrieve all installed services.

Code:

Option Explicit

Type MAPPING_SERVICE_INFO
    Size As Long
    pszCopyright As String
    wMajorVersion As Integer
    wMinorVersion As Integer
    wBuildVersion As Integer
    wStepVersion As Integer
    dwInputContentTypesCount As Long
    prgInputContentTypes As String
    dwOutputContentTypesCount As Long
    prgOutputContentTypes As String
    dwInputLanguagesCount As Long
    prgInputLanguages As String
    dwOutputLanguagesCount As Long
    prgOutputLanguages As String
    dwInputScriptsCount As Long
    prgInputScripts  As String
    dwOutputScriptsCount  As Long
    prgOutputScripts As String
    pguid As Long
    pszCategory  As String
    pszDescription As String
    dwPrivateDataSize  As Long
    pPrivateData As Long  'LPVOID
    pContext As Long  'LPVOID
    IsOneToOneLanguageMapping As Long
    HasSubservices As Long
    OnlineOnly As Long
    ServiceType As Long
End Type

Type MAPPING_DATA_RANGE
    dwStartIndex As Long
    dwEndIndex As Long
    pszDescription As String
    dwDescriptionLength As Long
    pData As Long  'LPVOID
    dwDataSize As Long
    pszContentType  As String
    prgActionIds As String
    dwActionsCount As Long
    prgActionDisplayNames As String
End Type

Type MAPPING_PROPERTY_BAG
    Size As Long
    prgResultRanges As MAPPING_DATA_RANGE
    dwRangesCount  As Long
    pServiceData As Long 'LPVOID
    dwServiceDataSize As Long
    pCallerData As Long  'LPVOID
    dwCallerDataSize As Long
    pContext  As Long  'LPVOID
End Type


'or VBE6.dll
Private Declare Function VarPtrArray Lib "VBE7" Alias _
    "VarPtr" (ByRef Var() As Any) As Long

Private Declare _
Function MappingFreeServices Lib "Elscore.dll" ( _
 ByVal pServiceInfo As Long _
) As Long

Private Declare _
Function MappingGetServices Lib "Elscore.dll" ( _
    pOptions As Any, _
    prgServices As Any, _
    pdwServicesCount As Long _
) As Long

Private Declare _
Function MappingRecognizeText Lib "Elscore.dll" ( _
    pServiceInfo As Any, _
  ByVal pszText As String, _
  ByVal dwLength As Long, _
    ByVal dwIndex As Long, _
  ByVal pOptions As Long, _
  pbag As Any _
) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)



Sub main()
    Const S_OK = 0
    Dim prgServices(9) As MAPPING_SERVICE_INFO
    Dim HRESULT As Long
   
   
    Dim ptrToArrayVar As Long
    Dim ptrToSafeArray As Long
    ptrToArrayVar = VarPtrArray(prgServices)
    CopyMemory ptrToSafeArray, ByVal ptrToArrayVar, 4
   
    Dim dwServicesCount As Long
    HRESULT = MappingGetServices(ByVal 0, ptrToSafeArray, dwServicesCount)
    Debug.Print HRESULT = S_OK, "....SUCCESS"
    Debug.Print dwServicesCount & " Services", "....SUCCESS"  '<== RETURNS 9 SERVICES.
   
    Const USER_TEXT_SKIP = 0
    Const USER_TEXT = "Hello world"
    Dim bag As MAPPING_PROPERTY_BAG
    bag.Size = Len(bag)
    HRESULT = MappingRecognizeText(prgServices(0), USER_TEXT, Len(USER_TEXT), USER_TEXT_SKIP, 0, bag)
    Debug.Print HRESULT = S_OK, "....FAILED !"  '<==  returns error code -2147024809
   
   
    HRESULT = MappingFreeServices(ptrToSafeArray)
    Debug.Print HRESULT = S_OK, "....SUCCESS"

End Sub

I am trying to follow and respect all instructions in the documentation for each API function but I must be doing something wrong:
MappingGetServices
MappingRecognizeText


Can someone please take a look and see why the call to MappingRecognizeText is failing ?

Regards.

Viewing all articles
Browse latest Browse all 15475

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>