hellovapi is a simple program that demonstrates how to use Viewing SDK to display documents in your application. The program creates a Windows application window, and then creates a child window that the Viewing SDK uses to display documents. The Viewing SDK is controlled by sending Windows-style messages to the child window. The set of messages that you send to the child window form the Viewing API (VAPI). The child window is known as a VAPI window. The VAPI messages are described in Message Parameters.
Before you can create a VAPI window, you must load the VAPI library (kvvapi.dll) by using the LoadLibrary function. In hellovapi, the library is loaded in the InitializeVAPI function which is called during the processing of the WM_CREATE message.
The library is loaded in the following way:
InitializeVAPI calls either the GetPrivateProfileString Windows function or RegQueryValueEx to get the value of HOME in the General section of the kvsdk.ini file or Windows registry.
GetPrivateProfileString gets the value from kvsdk.ini, and RegQueryValueEx gets the value from the registry. The HOME setting specifies the location of the Viewing SDK bin directory. InitializeVAPI demonstrates how to get this setting from both the registry and kvsdk.ini file. By default, InitializeVAPI gets the value from kvsdk.ini.
InitializeVAPI then creates the path to the VAPI library and loads it:
wsprintf (szDLLPath, TEXT("%s\\%s"), szHome, VAPIDF_VAPI_DLL_NAME);
hLibVAPI = LoadLibrary (szDLLPath);
After the VAPI library is loaded, the program creates the VAPI window by calling CreateWindow with a class name of VAPIDF_VAPI_WINDOW_CLASS_NAME:
hWndVAPI = CreateWindow (VAPIDF_VAPI_WINDOW_CLASS_NAME, NULL, WS_CHILD | WS_DISABLED, rc.left, rc.top, rc.right, rc.bottom, hWnd, NULL, hLibVAPI, &CreateParams);
The last parameter passes in a pointer to VAPI creation information. This creation information is in the structure of type TPVAPICreateParams, and tells VAPI where to locate the initialization settings, and whether they are in the registry or in kvsdk.ini. The hellovapi program uses the bUseRegistry global variable to specify whether to get the settings from the registry or kvsdk.ini.
By default, hellovapi tells VAPI to use the kvsdk.ini file that was located with the call to InitializeVAPI. The path to the kvsdk.ini file is stored in the szIniFileName global variable.
To specify the registry
Set uProfileType to PROFILEDF_USE_REG.
Set lpszRegistryName to the registry name of the Viewing SDK key under the main branch HKEY_LOCAL_MACHINE\SOFTWARE. The default is Autonomy\Viewing SDK.
To specify an initialization file
Set uProfileType to PROFILEDF_USE_INI.
Set lpszIniFileName to the location of the initialization file.
// Initialize the parameters for the creation of the VAPIwindow.//
memset (&CreateParams, 0, sizeof(TPVAPICreateParams));
if (bUseRegistry)
{
CreateParams.uProfileType = PROFILEDF_USE_REG;
CreateParams.lpszRegistryName = REGISTRY_NAME_ASCII;
}
else
{
CreateParams.uProfileType = PROFILEDF_USE_INI;
CreateParams.lpszIniFileName = szIniFileName;
}
To tell VAPI to open a document
Create a TPVAPIOpenDocInfoEx structure.
Send VAPI a VAPIM_INIT message with the wParam set to VAPIMWP_INIT_OPENDOCEX, and the lParam set to the address of the TPVAPIOpenDocInfoEx structure.
This is demonstrated in the OpenDoc function. It uses a Windows Open dialog box to get the name of a file, and sets the lpszFilePath parameter of the TPVAPIOpenDocInfoEx structure to this file name. If it is successful, the VAPIM_INIT message returns VAPI_RETURN_SUCCESS.
// Open the document.//
memset (&OpenDocInfo, 0, sizeof(TPVAPIOpenDocInfoEx));
OpenDocInfo.lpszFilePath = szFileName;
lResult = SendMessage (hWndVAPI, VAPIM_INIT, VAPIMWP_INIT_OPENDOCEX,
(LPARAM)&OpenDocInfo);
// hellovapi.c
This program demonstrates how to use the Viewing API (VAPI) to display documents.//
#ifdef UNICODE
#ifndef _UNICODE
#define _UNICODE
#endif
#endif
#include <windows.h>
#include "kvoem.h"
#include "hellovapi.h"
#define REGISTRY_NAME TEXT("Autonomy\\Viewing SDK")
#define REGISTRY_NAME_ASCII "Autonomy\\Viewing SDK"
HWND hWndVAPI = NULL; //VAPI window
HINSTANCE hLibVAPI = NULL; //VAPI library instance
BOOL bUseRegistry = FALSE; //Profile type (Set to FALSE for initialization file)
TCHAR szIniFileName[MAX_PATH];
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
void OpenDoc (HWND);
BOOL InitializeVAPI (void);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow)
{
static TCHAR szAppName[] = TEXT("Hello VAPI demo program");
HWND hWnd;
MSG msg;
WNDCLASSEX wc;
wc.cbSize = sizeof (wc);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.lpszClassName = szAppName;
wc.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
RegisterClassEx (&wc);
hWnd = CreateWindow (szAppName, TEXT("Hello VAPI"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
TPVAPICreateParams CreateParams;
RECT rc;
#ifdef UNICODE
char szName[MAX_PATH];
#endif
if (!InitializeVAPI())
{
return -1;
}
// Initialize parameters for creation of the VAPI window.//
memset (&CreateParams, 0, sizeof(TPVAPICreateParams));
if (bUseRegistry)
{
CreateParams.uProfileType = PROFILEDF_USE_REG;
CreateParams.lpszRegistryName = REGISTRY_NAME_ASCII;
}
else
{
CreateParams.uProfileType = PROFILEDF_USE_INI;
#ifdef UNICODE
WideCharToMultiByte (CP_ACP, 0, szIniFileName, -1, szName,
MAX_PATH, NULL, NULL);
CreateParams.lpszIniFileName = szName;
#else
CreateParams.lpszIniFileName = szIniFileName;
#endif
}
// Create the VAPI window. //
GetClientRect (hWnd, &rc);
hWndVAPI = CreateWindow (VAPIDF_VAPI_WINDOW_CLASS_NAME,
NULL,
WS_CHILD | WS_DISABLED,
rc.left, rc.top, rc.right, rc.bottom,
hWnd,
NULL,
hLibVAPI,
&CreateParams);
return 0;
}
case WM_DESTROY:
// Destroy the VAPI window. //
if (hWndVAPI != NULL)
{
DestroyWindow (hWndVAPI);
}
// Free the VAPI library. //
if (hLibVAPI != NULL)
{
FreeLibrary (hLibVAPI);
}
PostQuitMessage (0);
return 0;
case WM_CLOSE:
DestroyWindow (hWnd);
return 0;
case WM_SIZE:
MoveWindow (hWndVAPI, 0, 0, LOWORD(lParam), HIWORD(lParam),
TRUE);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_OPEN:
OpenDoc (hWnd);
return 0;
case IDM_CLOSE:
SendMessage (hWnd, WM_CLOSE, 0, 0);
return 0;
}
break;
}
return DefWindowProc (hWnd, uMsg, wParam, lParam);
}
void OpenDoc (HWND hWnd)
{
LRESULT lResult;
OPENFILENAME ofn;
TCHAR szFileName[MAX_PATH];
TPVAPIOpenDocInfoEx OpenDocInfo;
#ifdef UNICODE
char szName[MAX_PATH];
#endif
// Get a document name. //
szFileName[0] = TEXT('\0');
memset (&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
OFN_FILEMUSTEXIST;
if (GetOpenFileName (&ofn) == 0)
{
return;
}
// Open the document. //
memset (&OpenDocInfo, 0, sizeof(TPVAPIOpenDocInfoEx));
#ifdef UNICODE
WideCharToMultiByte (CP_ACP, 0, szFileName, -1, szName, MAX_PATH,
NULL, NULL);
OpenDocInfo.lpszFilePath = szName;
#else
OpenDocInfo.lpszFilePath = szFileName;
#endif
lResult = SendMessage (hWndVAPI, VAPIM_INIT,
VAPIMWP_INIT_OPENDOCEX, (LPARAM)&OpenDocInfo);
if (lResult != VAPI_RETURN_SUCCESS)
{
MessageBox (hWnd, TEXT("Unable to view document."),
TEXT("Hello VAPI"), MB_OK);
return;
}
return;
}
// Function:InitializeVAPI() //
// Summary: Load and initialize KVVAPI.dll for use with hellovapi. //
BOOL InitializeVAPI (void)
{
long lResult;
TCHAR szDLLPath[MAX_PATH];
TCHAR szHome[MAX_PATH];
// Get the location of the VAPI DLL.//
if (bUseRegistry)
{
HKEY hKey;
TCHAR szSubKey[256];
DWORD dwType;
DWORD dwcbData;
// Open the registry key. //
wsprintf (szSubKey, TEXT("SOFTWARE\\%s\\General"), REGISTRY_NAME);
lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0, KEY_READ, &hKey);
if (lResult != ERROR_SUCCESS)
{
return FALSE;
}
dwcbData = sizeof(szHome);
lResult = RegQueryValueEx (hKey, TEXT("HOME"), NULL, &dwType,
(PBYTE)szHome, &dwcbData);
RegCloseKey (hKey);
if (lResult != ERROR_SUCCESS)
{
return FALSE;
}
}
else
{
int nSize;
// Get the location of the initialization file. //
GetWindowsDirectory (szIniFileName, MAX_PATH);
_tcscat (szIniFileName, TEXT("\\kvsdk.ini"));
if (GetFileAttributes (szIniFileName) == 0xffffffff)
{
return FALSE;
}
nSize = GetPrivateProfileString (TEXT("General"), TEXT("HOME"),
NULL, szHome, MAX_PATH, szIniFileName);
if (nSize <= 0)
{
return FALSE;
}
}
// Load the VAPI DLL. //
wsprintf (szDLLPath, TEXT("%s\\%s"), szHome, VAPIDF_VAPI_DLL_NAME);
hLibVAPI = LoadLibrary (szDLLPath);
if (hLibVAPI == NULL)
{
return FALSE;
}
return TRUE;
}
// hellovapi.h // This file is the main header file for hellovapi.exe // Resource definitions #define IDR_MENU 100 #define IDM_OPEN 100 #define IDM_CLOSE 101
// hellovapi.rc resource script #include "hellovapi.h" // Menu IDR_MENU MENU DISCARDABLE BEGIN POPUP "&File" BEGIN MENUITEM "&Open", IDM_OPEN MENUITEM "&Exit", IDM_CLOSE END END
|
|