5,781,349 members and growing! (21,655 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Spell Checkers     Intermediate

Using the Microsoft Common Spell API

By Tibor Blazko

An introduction to using the Microsoft CSAPI.
VC6, C++Windows, NT4, Win2K, Visual Studio, Dev

Posted: 22 May 2001
Updated: 14 Jun 2001
Views: 99,247
Bookmarked: 28 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 5.60 Rating: 4.65 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 16.7%
4
5 votes, 83.3%
5

Introduction

Searching around in MSDN trying to find how to use the MS Office spell checker, I found a "fantastic" OLE solution:

  1. Run Word
  2. Push the word to be checked into Word
  3. Get the result.

Thanks very much :(

Then I found this. Through them, I got the Microsoft Common Spell API (CSAPI).

CSAPI

To ensure I respect copyright, I cut the smallest possible example using the default dictionary only, and ignored possible error states or exceptions. You will probably need to modify it for your language and paths (I'm not reading paths from registry).

Below is some sample code that illustrates how to use the SDK.

#include "windows.h"

#include "csapi.h" //with typedef unsigned char CHAR; removed


typedef WORD (*SpellInit_fn) (SPLID FAR *lpSid,
                              WSC FAR  *lpWsc);

typedef WORD (*SpellOpenMdr_fn)(SPLID    splid,
                                LPSPATH  lpspathMain,
                                LPSPATH  lpspathExc,
                                BOOL     fCreateUdrExc,
                                BOOL     fCache,
                                LID      lidExpected,
                                LPMDRS   lpMdrs);

typedef WORD (*SpellCheck_fn)(SPLID    splid,
                              SCCC     iScc,
                              LPSIB    lpSib,
                              LPSRB    lpSrb);

typedef WORD (*SpellCloseMdr_fn)(SPLID    splid,
                                 LPMDRS   lpMdrs);

typedef WORD (*SpellTerminate_fn)(SPLID    splid,
                                  BOOL     fForce);

void main(void)
{
    int Language = 1051; //slovak


    //to be more general search thru registry:

    //Dll functional interface:

    //  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\

    //           Proofing Tools\Spelling\1051\Normal\Engine

    //Mdr main directory:

    //  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\

    //           Proofing Tools\Spelling\1051\Normal\Dictionary

    //Under any user directory:

    //  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\

    //           Proofing Tools\Custom Dictionaries\1


    char DllName[] = 
     "C:\\Program Files\\Common Files\\Microsoft Shared\\Proof\\MSPSK32.DLL";
    char MdrName[] = 
     "C:\\Program Files\\Common Files\\Microsoft Shared\\Proof\\MSSP_SK.LEX";

    HMODULE SpellInstance = LoadLibrary(DllName);

    SpellInit_fn SpellInit = 
      (SpellInit_fn)GetProcAddress(SpellInstance, 
      "SpellInit");
    SpellOpenMdr_fn SpellOpenMdr = 
      (SpellOpenMdr_fn)GetProcAddress(SpellInstance, 
      "SpellOpenMdr");
    SpellCheck_fn SpellCheck = 
      (SpellCheck_fn)GetProcAddress(SpellInstance, 
      "SpellCheck");
    SpellCloseMdr_fn SpellCloseMdr = 
      (SpellCloseMdr_fn)GetProcAddress(SpellInstance, 
      "SpellCloseMdr");
    SpellTerminate_fn SpellTerminate = 
      (SpellTerminate_fn)GetProcAddress(SpellInstance, 
      "SpellTerminate");

    SPLID Handle;
    WSC SpecChars;
    SpecChars.bIgnore = 0;
    SpecChars.bHyphenHard = 45;
    SpecChars.bHyphenSoft = 31;
    SpecChars.bHyphenNonBreaking = 30;
    SpecChars.bEmDash = 151;
    SpecChars.bEnDash = 150;
    SpecChars.bEllipsis = 133;
    SpecChars.rgLineBreak[0] = 11;
    SpecChars.rgLineBreak[1] = 10;
    SpecChars.rgParaBreak[0] = 13;
    SpecChars.rgParaBreak[1] = 10;

    SpellInit(&Handle, &SpecChars);

    MDRS Mdrs;
    SpellOpenMdr(Handle, MdrName, NULL, FALSE, TRUE, Language, &Mdrs);

    SIB InputBuffer;
    InputBuffer.cMdr = 1;
    InputBuffer.lrgMdr = &Mdrs.mdr;
    InputBuffer.cUdr = 0;
    InputBuffer.lrgUdr = NULL;

    SRB ResultBuffer;
    ResultBuffer.cch = 1024;
    ResultBuffer.lrgsz = (char*)malloc(ResultBuffer.cch);
    ResultBuffer.cbRate = 255;
    ResultBuffer.lrgbRating = (unsigned char*)malloc(ResultBuffer.cbRate);

    InputBuffer.lrgch = "slovo"; //tested word

    InputBuffer.cch = strlen(InputBuffer.lrgch);
    InputBuffer.wSpellState = fssStartsSentence;
    SpellCheck(Handle, sccVerifyWord, &InputBuffer, &ResultBuffer);

    int Result = ResultBuffer.scrs; //scrsNoErrors


    free(ResultBuffer.lrgsz);
    free(ResultBuffer.lrgbRating);
    SpellCloseMdr(Handle, &Mdrs);
    SpellTerminate(Handle, TRUE);
    FreeLibrary(SpellInstance);
}

I have tried the above code with Office 95 and Office 97. Installing Office 2000 with English, Slovak and German languages creates a registry Spelling\1051 (slovak) and Grammar\1031 (german) entries.

BTW: My search for a similar WIN API for string comparison failed. It seems Windows knows how to use the actual language only (resp. see .NET Framework Reference, String.Compare Method (String, String, Boolean, CultureInfo)), and there is no way to hook between your application and the system to correct the MS bugs for "small markets" like my country (where exceptional vocabulary or similar solution are necessary for proper string evaluation). If you know more about this, do not hesitate to publish it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tibor Blazko



Occupation: Software Developer (Senior)
Location: Slovakia Slovakia

Other popular Algorithms & Recipes articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
GeneralWhat are the license termsmemberDwayne Bailey8:14 28 Jan '07  
GeneralProblem Creating a New Dictionary for MS Office Sort by Position Word Listmembermfcuser7:45 31 Aug '04  
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word ListmemberShaheer Ahmad23:23 7 Dec '04  
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word Listmembermfcuser3:48 8 Dec '04  
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word ListmemberShaheer Ahmad4:56 8 Dec '04  
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word Listmembermlavaud200523:21 12 May '05  
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word Listmembermertart23:18 14 May '05  
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word ListmemberRakeshShivanna21:50 5 Jun '06  
GeneralProblem running the code...memberdeepblue14:44 29 May '04  
GeneralRe: Problem running the code...memberTibor Blazko20:59 6 Jun '04  
GeneralRe: Problem running the code...memberalfonso.gonzalez6:55 14 Jul '05  
GeneralRe: Problem running the code...memberTibor Blazko6:59 15 Jul '05  
GeneralCSAPI 3 Dcoumentationmemberschler1:49 6 Jan '03  
GeneralCSAPI for Office 2000 and XP?memberAnonymous9:10 19 Apr '02  
GeneralRe: CSAPI for Office 2000 and XP?memberTibor Blazko2:39 7 May '02  
GeneralRe: CSAPI for Office 2000 and XP?memberLuzius Schneider4:49 15 May '02  
Generalspell checking problemmemberAnonymous2:22 13 Mar '02  
GeneralRe: spell checking problemmemberreal name4:14 13 Mar '02  
GeneralAddUdr solutionmemberTibor Blazko23:56 18 Mar '02  
Generaland another msdn spell referencememberreal name23:36 29 May '01  
GeneralNOTE : Third parties not licensed to use MS CSAPI.memberRob Pitt3:44 25 May '01  
GeneralRe: NOTE : Third parties not licensed to use MS CSAPI.memberJeff Pleimling10:12 4 Jun '01  
GeneralRe: NOTE : Third parties not licensed to use MS CSAPI.memberreal name1:30 11 Jun '01  
GeneralRe: NOTE : Third parties not licensed to use MS CSAPI.memberChris Maunder12:27 15 Jun '01  
GeneralRe: NOTE : Third parties not licensed to use MS CSAPI.memberMak Thorpe20:26 23 Jul '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Jun 2001
Editor: Smitha Vijayan
Copyright 2001 by Tibor Blazko
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project