// ========================================================================== // Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All // rights reserved. // // The coded instructions, statements, computer programs, and/or related // material (collectively the "Data") in these files contain unpublished // information proprietary to Autodesk, Inc. ("Autodesk") and/or its // licensors, which is protected by U.S. and Canadian federal copyright // law and by international treaties. // // The Data is provided for use exclusively by You. You have the right // to use, modify, and incorporate this Data into other products for // purposes authorized by the Autodesk software license agreement, // without fee. // // The copyright notices in the Software and this entire statement, // including the above license grant, this restriction and the // following disclaimer, must be included in all copies of the // Software, in whole or in part, and all derivative works of // the Software, unless such copies or derivative works are solely // in the form of machine-executable object code generated by a // source language processor. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. // AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED // WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF // NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR // PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR // TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS // BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL, // DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK // AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY // OR PROBABILITY OF SUCH DAMAGES. // // ========================================================================== #include "stdafx.h" #include "Common.h" /* * Parses the lpCmdLine argument as a mel: URL and attempts to send a command * to default Maya command port (named "commandportDefault"). * * The format of the URL is mel:[//[]][/] * * Currently, there is no attempt to launch Maya if it isn't already running, * nor is there any kind of error reporting. */ extern "C" void CALLBACK OpenURLW(HWND hwnd, HINSTANCE hinst, LPCWSTR lpCmdLine, int nCmdShow) { CAtlString strUrl(lpCmdLine); // for example, convert "%20" sequences back to spaces HRESULT hr = ::UrlUnescape(strUrl.GetBuffer(), NULL, NULL, URL_UNESCAPE_INPLACE); if (FAILED(hr)) return; strUrl.ReleaseBuffer(); CAtlRegExp<> re; REParseError status = re.Parse(L"[Mm][Ee][Ll]:(//({[^/]*?}/)?)?{.+}"); if (status != REPARSE_ERROR_OK) return; // specified URL must match regular expression CAtlREMatchContext<> mc; if (!re.Match(strUrl, &mc) || mc.m_uNumGroups != 2) return; // extract command and optional port name CAtlREMatchContext::MatchGroup mg; mc.GetMatch(0, &mg); CAtlString strPortName(mg.szStart, (int)(ptrdiff_t)(mg.szEnd - mg.szStart)); mc.GetMatch(1, &mg); CAtlString strCommand(mg.szStart, (int)(ptrdiff_t)(mg.szEnd - mg.szStart)); // use default port name if not specified if (strPortName.IsEmpty()) strPortName = L"commandportDefault"; // trim trailing slash character(s) strCommand.TrimRight(L"/"); // convert Unicode strings to ANSI CW2A pszPortName(strPortName); CW2A pszCommand(strCommand); // initialize Windows Sockets 2 WSADATA wsaData; int err = WSAStartup(MAKEWORD(2, 0), &wsaData); if (err != 0) return; ADDRINFO *pAddrInfo; if (!GetMayaCommandPortAddress(pszPortName, &pAddrInfo)) { WSACleanup(); return; } SOCKET sock = ConnectToMayaCommandPortByAddress(pAddrInfo); freeaddrinfo(pAddrInfo); if (sock == INVALID_SOCKET) { WSACleanup(); return; } // send command to Maya command port err = send(sock, pszCommand, (int)strlen(pszCommand), 0); if (err == SOCKET_ERROR) { DisconnectFromMayaCommandPort(sock); WSACleanup(); return; } // receive reply, if any, but then just discard it... char* recvBuf = new char[4096+1]; int numBytes = recv(sock, recvBuf, 4096, 0); delete[] recvBuf; DisconnectFromMayaCommandPort(sock); WSACleanup(); } extern "C" void CALLBACK OpenURLA(HWND hwnd, HINSTANCE hinst, LPCSTR lpCmdLine, int nCmdShow) { // convert ANSI command line to Unicode and call wide-character function OpenURLW(hwnd, hinst, CA2W(lpCmdLine), nCmdShow); }