Click here to Skip to main content
11,639,847 members (59,543 online)
Click here to Skip to main content

Using the Microsoft Common Spell API

, 14 Jun 2001 152.2K 2.4K 32
Rate this:
Please Sign up or sign in to vote.
An introduction to using the Microsoft CSAPI.

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

Share

About the Author

Tibor Blazko
Software Developer (Senior)
Slovakia Slovakia
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionhow used it? Pin
rame remo15-Dec-14 22:19
memberrame remo15-Dec-14 22:19 
AnswerRe: how used it? Pin
Tibor Blazko15-Dec-14 22:27
memberTibor Blazko15-Dec-14 22:27 
GeneralRe: how used it? Pin
rame remo17-Dec-14 0:35
memberrame remo17-Dec-14 0:35 
GeneralRe: how used it? Pin
Tibor Blazko20-Dec-14 19:56
memberTibor Blazko20-Dec-14 19:56 
Questionlink: Using the C++ Spell Checking API Pin
Tibor Blazko11-Aug-13 21:18
memberTibor Blazko11-Aug-13 21:18 
GeneralCSAPI3 Pin
leebauer2-Nov-09 0:55
memberleebauer2-Nov-09 0:55 
Questionhow to complile Pin
leebauer21-Oct-09 6:21
memberleebauer21-Oct-09 6:21 
AnswerRe: how to complile Pin
Tibor Blazko25-Oct-09 0:04
memberTibor Blazko25-Oct-09 0:04 
Generalcreating local language spell cheking Pin
tedkidane1-Oct-09 7:39
membertedkidane1-Oct-09 7:39 
GeneralRe: creating local language spell cheking Pin
Tibor Blazko1-Oct-09 9:21
memberTibor Blazko1-Oct-09 9:21 
GeneralUsing unicode chars Pin
mita5555526-Aug-09 1:34
membermita5555526-Aug-09 1:34 
GeneralRe: Using unicode chars Pin
Tibor Blazko26-Aug-09 1:39
memberTibor Blazko26-Aug-09 1:39 
QuestionWhat are the license terms Pin
Dwayne Bailey28-Jan-07 7:14
memberDwayne Bailey28-Jan-07 7:14 
GeneralProblem Creating a New Dictionary for MS Office Sort by Position Word List Pin
mfcuser31-Aug-04 6:45
membermfcuser31-Aug-04 6:45 
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word List Pin
Shaheer Ahmad7-Dec-04 22:23
memberShaheer Ahmad7-Dec-04 22:23 
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word List Pin
mfcuser8-Dec-04 2:48
membermfcuser8-Dec-04 2:48 
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word List Pin
Shaheer Ahmad8-Dec-04 3:56
memberShaheer Ahmad8-Dec-04 3:56 
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word List Pin
mlavaud200512-May-05 22:21
membermlavaud200512-May-05 22:21 
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word List Pin
mertart14-May-05 22:18
membermertart14-May-05 22:18 
GeneralRe: Problem Creating a New Dictionary for MS Office Sort by Position Word List Pin
RakeshShivanna5-Jun-06 20:50
memberRakeshShivanna5-Jun-06 20:50 
GeneralProblem running the code... Pin
deepblue129-May-04 3:44
memberdeepblue129-May-04 3:44 
GeneralRe: Problem running the code... Pin
Tibor Blazko6-Jun-04 19:59
memberTibor Blazko6-Jun-04 19:59 
GeneralRe: Problem running the code... Pin
alfonso.gonzalez14-Jul-05 5:55
memberalfonso.gonzalez14-Jul-05 5:55 
GeneralRe: Problem running the code... Pin
Tibor Blazko15-Jul-05 5:59
memberTibor Blazko15-Jul-05 5:59 
GeneralCSAPI 3 Dcoumentation Pin
schler6-Jan-03 0:49
memberschler6-Jan-03 0:49 
QuestionCSAPI for Office 2000 and XP? Pin
Anonymous19-Apr-02 8:10
memberAnonymous19-Apr-02 8:10 
AnswerRe: CSAPI for Office 2000 and XP? Pin
Tibor Blazko7-May-02 1:39
memberTibor Blazko7-May-02 1:39 
GeneralRe: CSAPI for Office 2000 and XP? Pin
Luzius Schneider15-May-02 3:49
memberLuzius Schneider15-May-02 3:49 
Generalspell checking problem Pin
Anonymous13-Mar-02 1:22
memberAnonymous13-Mar-02 1:22 
GeneralRe: spell checking problem Pin
real name13-Mar-02 3:14
memberreal name13-Mar-02 3:14 
GeneralAddUdr solution Pin
Tibor Blazko18-Mar-02 22:56
memberTibor Blazko18-Mar-02 22:56 
Generaland another msdn spell reference Pin
real name29-May-01 22:36
memberreal name29-May-01 22:36 
GeneralNOTE : Third parties not licensed to use MS CSAPI. Pin
Rob Pitt25-May-01 2:44
memberRob Pitt25-May-01 2:44 
GeneralRe: NOTE : Third parties not licensed to use MS CSAPI. Pin
Jeff Pleimling4-Jun-01 9:12
memberJeff Pleimling4-Jun-01 9:12 
GeneralRe: NOTE : Third parties not licensed to use MS CSAPI. Pin
real name11-Jun-01 0:30
memberreal name11-Jun-01 0:30 
GeneralRe: NOTE : Third parties not licensed to use MS CSAPI. Pin
Chris Maunder15-Jun-01 11:27
memberChris Maunder15-Jun-01 11:27 
GeneralRe: NOTE : Third parties not licensed to use MS CSAPI. Pin
Mak Thorpe23-Jul-02 19:26
memberMak Thorpe23-Jul-02 19:26 
GeneralRe: NOTE : Third parties not licensed to use MS CSAPI. Pin
Rob Pitt23-Jul-02 22:11
memberRob Pitt23-Jul-02 22:11 
GeneralRe: NOTE : Third parties not licensed to use MS CSAPI. Pin
TeaShirt1-Jul-03 3:43
memberTeaShirt1-Jul-03 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 15 Jun 2001
Article Copyright 2001 by Tibor Blazko
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid