//プロジェクト名directprn001 C++ソースファイルdirectprn001.cpp //EXTERN CDECL CVOID directprn(CSTRING,CINT,CINT,CINT,CINT) directprn001.dll //("プリンタ名",PAPERSIZE,ORIENTATION,DUPLEX,印刷ダイアログ0非表示1表示) //directprn(char * printername01,int papersize01,int orientation01,int duplex01,int dlg01) //directprn("",9,1,2,0) //PRINT FORM form01 //PRINT FORM //PRINT FORM END //プリンタ名 ""はデフォルト //用紙サイズ 0はデフォルト //DMPAPER_A3 = 8 //DMPAPER_A4 = 9 //DMPAPER_A4SMALL = 10 //DMPAPER_A5 = 11 //DMPAPER_B4 = 12 //DMPAPER_B5 = 13 //DMPAPER_JAPANESE_POSTCARD = 43 //用紙方向 0はデフォルト //DMORIENT_PORTRAIT = 1 //DMORIENT_LANDSCAPE = 2 //両面印刷 0はデフォルト //DMDUP_SIMPLEX = 1 //DMDUP_VERTICAL = 2 //DMDUP_HORIZONTAL = 3 //印刷ダイアログ 0は非表示 1は表示 //isshift_jisの戻り値は 0:1バイト文字 1:Shift JIS1バイト目 2:Shift JIS2バイト目 #include static char tempa[255]; static char tempb[255]; static char tempc[8192]; char * defaultprn = tempa; char * pDevice = tempb; LPDEVMODE pDevMode = (LPDEVMODE)tempc; int init01 = 1; int init02 = 1; char * printername01 = NULL; int papersize01,orientation01,duplex01,dlg01,flag01,notfound; int a1,a2,a3,a4; int pre1,pre2,pre3,pre4; DWORD oldprotect; long hdc01; int (__stdcall * printa)(struct tagPDA *); CONST DOCINFO *lpdi; int __stdcall temp01(HDC hdc,CONST DOCINFO *lpdi); int __stdcall temp02(struct tagPDA &pd); int __stdcall temp01(HDC hdc,CONST DOCINFO *lpdi) { hdc01=(long)hdc; VirtualProtect((LPVOID)a1,16,PAGE_EXECUTE_READWRITE,&oldprotect); __asm { mov esi,a1 mov eax,pre1 mov [esi],eax mov eax,pre2 mov [esi+4],eax } VirtualProtect((LPVOID)a1,16,PAGE_EXECUTE,&oldprotect); FlushInstructionCache((HANDLE)a1,(LPCVOID)a1,16); init01 = 1; int n=StartDoc( hdc , lpdi); return n; } int __stdcall temp02(struct tagPDA &pd) { VirtualProtect((LPVOID)a3,16,PAGE_EXECUTE_READWRITE,&oldprotect); __asm { mov esi,a3 mov eax,pre3 mov [esi],eax mov eax,pre4 mov [esi+4],eax } VirtualProtect((LPVOID)a3,16,PAGE_EXECUTE,&oldprotect); FlushInstructionCache((HANDLE)a3,(LPCVOID)a3,16); if ((flag01 == 0) && (* printername01 != NULL)) { char * param1,* param2; param1 = (char *)calloc(1,255); param2 = (char *)calloc(1,255); GetProfileString("devices", printername01, "error", param1, 250); if (strcmp(param1,"error") == 0) notfound = 1; else { wsprintf(param2, "%s,%s", printername01, param1); WriteProfileString("windows","device", param2); // SendMessageTimeout(HWND_BROADCAST, WM_WININICHANGE, 0L, (LPARAM)(LPCTSTR)"windows", SMTO_NORMAL, 1000, NULL); flag01 = 1; } free(param1); free(param2); } GetProfileString("windows", "device", "error", pDevice, 250); char * index02 = strstr(pDevice,","); * index02 = '\0'; HANDLE hPrinter; HWND hWnd=GetForegroundWindow(); if (!OpenPrinter(pDevice, &hPrinter, NULL)) { if (flag01 == 1) { WriteProfileString("windows","device", defaultprn); } return 0; } DWORD dwRet = DocumentProperties(hWnd, hPrinter, pDevice, pDevMode, NULL, DM_OUT_BUFFER); if (dwRet != IDOK) { ClosePrinter(hPrinter); if (flag01 == 1) { WriteProfileString("windows","device", defaultprn); } return 0; } if (pDevMode->dmFields & DM_PAPERSIZE) { if (papersize01 != 0) { pDevMode->dmPaperSize = papersize01; } } if (pDevMode->dmFields & DM_ORIENTATION) { if (orientation01 != 0) { pDevMode->dmOrientation = orientation01; } } if (pDevMode->dmFields & DM_DUPLEX) { if (duplex01 != 0) { pDevMode->dmDuplex = duplex01; } } dwRet = DocumentProperties(hWnd, hPrinter, pDevice, pDevMode, pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER); ClosePrinter(hPrinter); if (dwRet != IDOK) { if (flag01 == 1) { WriteProfileString("windows","device", defaultprn); } return 0; } int n; if ((pd.Flags & PD_RETURNDEFAULT)!=0) { n=(int)printa(&pd); VirtualProtect((LPVOID)a3,16,PAGE_EXECUTE_READWRITE,&oldprotect); __asm { mov esi,a3 mov al,0xB8 mov [esi],al mov eax,a4 mov [esi+1],eax mov ax,0xC350 mov [esi+5],ax } VirtualProtect((LPVOID)a3,16,PAGE_EXECUTE,&oldprotect); FlushInstructionCache((HANDLE)a3,(LPCVOID)a3,16); } else { if (notfound) { init02 = 1; return 0; } if (dlg01 == 0) { pd.hDevMode=0; pd.hDevNames=0; pd.Flags=(pd.Flags | PD_RETURNDEFAULT); init02 = 1; n=(int)printa(&pd); ResetDC(pd.hDC,pDevMode); } else { pd.hDevMode=pDevMode; init02 = 1; n=(int)printa(&pd); } if (flag01 == 1) { WriteProfileString("windows","device", defaultprn); } } return n; } extern "C" { __declspec(dllexport) void directprn(char * printername02,int papersize02,int orientation02,int duplex02,int dlg02); __declspec(dllexport) int hookstartdoc01(void); __declspec(dllexport) long gethookdc01(void); __declspec(dllexport) int startdoc01(long hDC,char * string01); __declspec(dllexport) int isshift_jis(char * string01,int n); void directprn(char * printername02,int papersize02,int orientation02,int duplex02,int dlg02) { if (init02) { GetProfileString("windows", "device", "error", defaultprn, 250); printername01 = printername02; papersize01 = papersize02; orientation01 = orientation02; duplex01 = duplex02; dlg01 = dlg02; flag01 = 0; notfound = 0; HMODULE hcomdlg32; hcomdlg32 = LoadLibrary("comdlg32.dll"); a3 = (int)GetProcAddress(hcomdlg32, "PrintDlgA"); printa = (int (__stdcall *)(struct tagPDA *))GetProcAddress(hcomdlg32, "PrintDlgA"); FreeLibrary(hcomdlg32); a4=(int)temp02; VirtualProtect((LPVOID)a3,16,PAGE_EXECUTE_READWRITE,&oldprotect); __asm { mov esi,a3 mov eax,[esi] mov pre3,eax mov eax,[esi+4] mov pre4,eax mov al,0xB8 mov [esi],al mov eax,a4 mov [esi+1],eax mov ax,0xC350 mov [esi+5],ax } VirtualProtect((LPVOID)a3,16,PAGE_EXECUTE,&oldprotect); FlushInstructionCache((HANDLE)a3,(LPCVOID)a3,16); init02 = 0; } return; } int hookstartdoc01(void) { if (init01) { a1=(int)StartDoc; a2=(int)temp01; VirtualProtect((LPVOID)a1,16,PAGE_EXECUTE_READWRITE,&oldprotect); __asm { mov esi,a1 mov eax,[esi] mov pre1,eax mov eax,[esi+4] mov pre2,eax mov al,0xB8 mov [esi],al mov eax,a2 mov [esi+1],eax mov ax,0xC350 mov [esi+5],ax } VirtualProtect((LPVOID)a1,16,PAGE_EXECUTE,&oldprotect); FlushInstructionCache((HANDLE)a1,(LPCVOID)a1,16); init01 = 0; } return (1); } long gethookdc01(void) { return (hdc01); } int startdoc01(long hDC,char * string01) { DOCINFO info01; info01.cbSize=20; info01.lpszDocName=string01; info01.lpszOutput=0; info01.lpszDatatype=0; info01.fwType=0; StartDoc((HDC)hDC,&info01); return (1); } int isshift_jis(char * string01,int n) { int len,c; unsigned char shift01; len=strlen(string01); if ((n < 1) || (n > len)) { return -1 ; } c = 0; while (c < len) { shift01=string01[c]; if (((shift01>= 0x81) && (shift01<=0x9F)) || ((shift01>=0xE0) && (shift01<=0xEF))) { if (c == n-1) { return 1 ; } if (c == n-2) { return 2 ; } c = c+2 ; } else { if (c == n-1) { return 0 ; } c = c+1 ; } } return -1 ; } }