#include <windows.h>

int FindWord(char *buf)
{
   HKEY key ;
   if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,"software\\microsoft\\Office",
         0,KEY_READ,&key) == ERROR_SUCCESS) {
      int index = 0 ;
      char name[1024] ;
      int size = 256 ;
      FILETIME t ;
      int rv ;
      do {
         memset(name,0,1024) ;
         size = 256 ;
         rv = RegEnumKeyEx(key,index++,name,&size,0,NULL,NULL,&t) ;
         if (rv == ERROR_SUCCESS || rv == ERROR_MORE_DATA) {
            HKEY key2 ;
            strcat(name,"\\word\\InstallRoot") ;
            if (RegOpenKeyEx(key,name,0,KEY_READ, &key2) == ERROR_SUCCESS) {
              size = 256 ;
              if (RegQueryValueEx(key2,"Path",0,NULL,buf,&size) == ERROR_SUCCESS) {
                RegCloseKey(key2) ;
                RegCloseKey(key) ;
								if (buf[0] == '"') {
									strcpy(buf,buf+1) ;
									buf[strlen(buf)-1] = 0 ;
								}
                if (buf[strlen(buf)-1] != '\\')
                  strcat(buf,"\\") ;
								strcat(buf,"winword.exe") ;
								return TRUE ;
							}
							RegCloseKey(key2) ;
            }
         }
      } while (rv == ERROR_SUCCESS || rv == ERROR_MORE_DATA) ;
      RegCloseKey(key) ;
   }
   return FALSE ;
}
int main(int argc, char *argv[])
{
	char buf[256] ;
	char argbuf[1024] ;
	if (argc != 2) {
		printf("need a file to open in WinWord") ;
		exit(1) ;
	}
	if (FindWord(buf)) {
		PROCESS_INFORMATION pi ;
		STARTUPINFO si ;
		sprintf(argbuf,"\"%s\" %s",buf,argv[1]) ;
		memset(&si,0,sizeof(STARTUPINFO)) ;
		si.cb = sizeof(STARTUPINFO) ;
		if (!CreateProcess(buf,argbuf,0,0,FALSE,0,0,0,&si,&pi))
			printf("Could not run winword.exe") ;
		else {
			WaitForSingleObject(pi.hProcess,INFINITE) ;
			CloseHandle(pi.hProcess) ;
			CloseHandle(pi.hThread) ;
			printf("success!") ;
		}
	} else
		printf("Could not find WinWord.EXE") ;
}