📄 winapiwrapper.cpp
字号:
const char *filePathStr = (const char*)env->GetByteArrayElements(filePath, NULL); const char *verbStr = (const char*)env->GetByteArrayElements(verb, NULL); HINSTANCE retval = ShellExecute(NULL, verbStr, filePathStr, NULL, NULL, SW_SHOWNORMAL); env->ReleaseByteArrayElements(filePath, (signed char*)filePathStr, 0); env->ReleaseByteArrayElements(verb, (signed char*)verbStr, 0); return (jint) retval;}char * jstringToUnicode(JNIEnv * env, jstring jstr){ int length = env->GetStringLength(jstr); const unsigned short * jcstr = env->GetStringChars(jstr, 0); char * ustr = (char *) malloc (length*2 + 1); int size = 0; size = WideCharToMultiByte(CP_ACP, 0, (unsigned short *)jcstr, length, ustr, (length*2+1), NULL, NULL); if (!size) return NULL; env->ReleaseStringChars(jstr, jcstr); ustr[size]='\0'; return ustr;}JNIEXPORT void JNICALL Java_org_jdesktop_jdic_desktop_internal_impl_WinAPIWrapper_openMapiMailer(JNIEnv * env, jclass obj, jobjectArray toArray, jobjectArray ccArray, jobjectArray bccArray, jstring subject, jstring body, jobjectArray attachArray){ HINSTANCE hlibMAPI = 0; unsigned long lhSession, ulResult; LPMAPILOGON lpMapiLogon; LPMAPISENDMAIL lpMapiSendMail; LPMAPILOGOFF lpMapiLogoff; char currentDir[MAX_PATH]; GetCurrentDirectory(MAX_PATH, currentDir); hlibMAPI = LoadLibrary("mapi32.dll"); if (hlibMAPI) { lpMapiLogon = (LPMAPILOGON) GetProcAddress (hlibMAPI, "MAPILogon"); lpMapiSendMail = (LPMAPISENDMAIL) GetProcAddress (hlibMAPI, "MAPISendMail"); ulResult = lpMapiLogon (0 , NULL, NULL, MAPI_LOGON_UI, 0, &lhSession); } if(ulResult) { jclass excCls = env->FindClass(EXCEPTION_CLASS); if(excCls == 0) { printf("Cannot find class LaunchFailedException\n"); return; } switch (ulResult) { case MAPI_E_FAILURE: env->ThrowNew(excCls, "Logon failed: One or more unspecified errors occurred during logon"); break; case MAPI_E_INSUFFICIENT_MEMORY: env->ThrowNew(excCls, "Logon failed: There was insufficient memory to proceed"); break; case MAPI_E_LOGIN_FAILURE: env->ThrowNew(excCls, "Logon failed: There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed"); break; case MAPI_E_TOO_MANY_SESSIONS: env->ThrowNew(excCls, "Logon failed: The user had too many sessions open simultaneously"); break; case MAPI_E_USER_ABORT: env->ThrowNew(excCls, "Logon failed: The user canceled the logon dialog box"); break; default: env->ThrowNew(excCls, "Logon failed: Unexpected exception"); } } if(lhSession) { MapiMessage msgSend; memset(&msgSend, 0, sizeof(MapiMessage)); if (subject) msgSend.lpszSubject = jstringToUnicode(env, subject); // Set Recip list to MapiMessage structure int toLen = 0; if(toArray) toLen = env->GetArrayLength(toArray); int ccLen = 0; if(ccArray) ccLen = env->GetArrayLength(ccArray); int bccLen = 0; if(bccArray) bccLen = env->GetArrayLength(bccArray); int length = toLen + ccLen + bccLen; if (length) { lpMapiRecipDesc recipSend; msgSend.nRecipCount = 0; recipSend = (lpMapiRecipDesc) malloc (length * sizeof(MapiRecipDesc)); memset(recipSend, 0, length*sizeof(MapiRecipDesc)); for(int i = 0; i < toLen; i ++) { jstring to = (jstring) env->GetObjectArrayElement(toArray, i); if (to) { char * temp = jstringToUnicode(env, to); recipSend[msgSend.nRecipCount].lpszName = temp; recipSend[msgSend.nRecipCount].lpszAddress = strdup(temp); recipSend[msgSend.nRecipCount].ulRecipClass = MAPI_TO; msgSend.nRecipCount ++; } } for(i = 0; i < ccLen; i ++) { jstring cc = (jstring) env->GetObjectArrayElement(ccArray, i); if (cc) { char * temp = jstringToUnicode(env, cc); recipSend[msgSend.nRecipCount].lpszName = temp; recipSend[msgSend.nRecipCount].lpszAddress = strdup(temp); recipSend[msgSend.nRecipCount].ulRecipClass = MAPI_CC; msgSend.nRecipCount ++; } } for(i = 0; i < bccLen; i ++) { jstring bcc = (jstring) env->GetObjectArrayElement(bccArray, i); if (bcc) { char * temp = jstringToUnicode(env, bcc); recipSend[msgSend.nRecipCount].lpszName = temp; recipSend[msgSend.nRecipCount].lpszAddress = strdup(temp); recipSend[msgSend.nRecipCount].ulRecipClass = MAPI_BCC; msgSend.nRecipCount ++; } } msgSend.lpRecips = recipSend; } // Set body to MapiMessage structure if (body) msgSend.lpszNoteText = jstringToUnicode(env, body); // Fix the Single-html-attachment problem for the Outlook mailer else msgSend.lpszNoteText = " "; //Set attachments to MapiMessage structure int attachLen = 0; if(attachArray) attachLen = env->GetArrayLength(attachArray); if (attachLen) { lpMapiFileDesc fileSend; msgSend.nFileCount = 0; fileSend = (lpMapiFileDesc) malloc (attachLen * sizeof(MapiFileDesc)); memset(fileSend, 0, attachLen*sizeof(MapiFileDesc)); for(int i = 0; i < attachLen; i++) { jstring file = (jstring) env->GetObjectArrayElement(attachArray, i); if (file) { fileSend[i].lpszPathName = jstringToUnicode(env, file); fileSend[i].nPosition = -1; msgSend.nFileCount++; } } msgSend.lpFiles = fileSend; } // open message compose window with Mapi API ulResult = lpMapiSendMail(lhSession, 0, &msgSend, MAPI_DIALOG | MAPI_LOGON_UI, 0L); if(ulResult) { jclass excCls = env->FindClass(EXCEPTION_CLASS); if(excCls == 0) { printf("Cannot find class LaunchFailedException\n"); return; } switch (ulResult) { case MAPI_E_AMBIGUOUS_RECIPIENT: env->ThrowNew(excCls, "A recipient matched more than one of the recipient descriptor structures"); break; case MAPI_E_ATTACHMENT_NOT_FOUND: env->ThrowNew(excCls, "The specified attachment was not found"); break; case MAPI_E_ATTACHMENT_OPEN_FAILURE: env->ThrowNew(excCls, "The specified attachment could not be opened"); break; case MAPI_E_BAD_RECIPTYPE: env->ThrowNew(excCls, "The type of a recipient was not MAPI_TO, MAPI_CC, or MAPI_BCC"); break; case MAPI_E_FAILURE: env->ThrowNew(excCls, "One or more unspecified errors occurred."); break; case MAPI_E_INSUFFICIENT_MEMORY: env->ThrowNew(excCls, "There was insufficient memory to proceed"); break; case MAPI_E_INVALID_RECIPS: env->ThrowNew(excCls, "One or more recipients were invalid or did not resolve to any address"); break; case MAPI_E_LOGIN_FAILURE: env->ThrowNew(excCls, "There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed"); break; case MAPI_E_TEXT_TOO_LARGE: env->ThrowNew(excCls, "The text in the message was too large"); break; case MAPI_E_TOO_MANY_FILES: env->ThrowNew(excCls, "There were too many file attachments"); break; case MAPI_E_TOO_MANY_RECIPIENTS: env->ThrowNew(excCls, "There were too many recipients"); break; case MAPI_E_UNKNOWN_RECIPIENT: env->ThrowNew(excCls, "A recipient did not appear in the address list"); break; case MAPI_E_USER_ABORT: break; default: env->ThrowNew(excCls, "One or more undefined errors occurred."); } } } lpMapiLogoff = (LPMAPILOGOFF) GetProcAddress (hlibMAPI, "MAPILogoff"); lpMapiLogoff(lhSession, 0, 0, 0); if (hlibMAPI) { FreeLibrary (hlibMAPI); } SetCurrentDirectory(currentDir);} #ifdef __cplusplus}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -