#include #include #include #include "stringutils.h" #include "itsutils.h" #include #include const std::string g_devicedllpath= "\\windows\\itsutils.dll"; HRESULT ItsutilsInvoke(const std::string& method, DWORD insize, BYTE *inbuf, DWORD *poutsize, BYTE **outbuf) { return CeRapiInvoke(ToWString(g_devicedllpath).c_str(), ToWString(method).c_str(), insize, inbuf, poutsize, outbuf, NULL, 0); } bool ITDialNumber(const std::string& number) { DialNumberParams dialnr; strcpy(dialnr.number, number.c_str()); DWORD outsize=0; HRESULT res= ItsutilsInvoke("ITDialNumber", sizeof(DialNumberParams ), reinterpret_cast(&dialnr), &outsize, NULL); if (res) { fprintf(stderr, "error dialing nr: %08lx\n", res); return false; } return true; } bool ITGetVersion(int *pVersion) { DWORD outsize=0; HRESULT res= ItsutilsInvoke("ITGetVersion", 0, NULL, &outsize, NULL); if (FAILED(res)) { return false; } *pVersion= res; return true; } bool CeCopyFileToDevice(const std::string& srcfile, const std::string& dstfile, bool bOverwrite) { struct stat sb; if (-1==stat(srcfile.c_str(), &sb)) { fprintf(stderr, "Source/host file does not exist '%s'\n", srcfile.c_str()); return false; } if ((sb.st_mode&S_IFMT)==S_IFDIR) { fprintf(stderr, "Source/host file specifies a directory '%s'\n", srcfile.c_str()); return false; } std::string dstname= dstfile; bool bExists= false; DWORD dwAttr = CeGetFileAttributes( ToWString(dstname).c_str()); if (0xFFFFFFFF != dwAttr) { if (dwAttr & FILE_ATTRIBUTE_DIRECTORY) { dstname += "\\"; size_t lastsl= srcfile.find_last_of("\\/"); if (lastsl!=std::string::npos) lastsl++; else lastsl=0; dstname += ToString(srcfile.substr(lastsl)); dwAttr = CeGetFileAttributes( ToWString(dstname).c_str()); // File already exists. -> true // dstname is directory -> false ( meaning something like \windows\dllname.dll is a directory ) // TODO: fix ambigous if .. if else if ((dwAttr != 0xFFFFFFFF)) { if (dwAttr & FILE_ATTRIBUTE_DIRECTORY) { fprintf(stderr, "ERROR: CeCopyFileToDevice to directory: %s\n", dstname.c_str()); return false; } else bExists= true; } else bExists= false; } else bExists= true; } if (!bOverwrite && bExists) { printf("NOTE: CeCopyFileToDevice not overwriting file %s\n", dstname.c_str()); return true; } FILE *fSrc= fopen(srcfile.c_str(), "rb"); if (NULL == fSrc) { fprintf(stderr, "Unable to open source/host file '%s': %s\n", srcfile.c_str(), strerror(errno)); return false; } HANDLE hDest = CeCreateFile( ToWString(dstname).c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hDest ) { fprintf(stderr, "Unable to open WinCE file '%s': %08lx\n", dstname.c_str(), CeGetLastError()); return false; } printf("Copying %s to WCE:%s\n", srcfile.c_str(), dstname.c_str()); ByteVector buffer; buffer.resize(32768); int dwNumRead; while (!feof(fSrc)) { dwNumRead= fread( vectorptr(buffer), 1, buffer.size(), fSrc); if (dwNumRead==-1) { fprintf(stderr, "Error !!! Reading source file: %s\n", strerror(errno)); CeCloseHandle(hDest); fclose(fSrc); return false; } DWORD dwNumWritten; if (!CeWriteFile( hDest, vectorptr(buffer), dwNumRead, &dwNumWritten, NULL)) { fprintf(stderr, "Error !!! Writing WinCE file: %08lx\n", CeGetLastError()); CeCloseHandle( hDest); fclose(fSrc); return false; } } CeCloseHandle( hDest); fclose(fSrc); return true; } bool install_itsutils() { return CeCopyFileToDevice("itsutils.dll", g_devicedllpath, true); } int main(int argc, char **argv) { if (argc!=2) { fprintf(stderr, "usage: pdial +31612345678\n"); return 1; } int res=0; HRESULT hr= CeRapiInit(); if (FAILED(hr)) { fprintf(stderr, "rapi init Failed - %08lx\n", hr); return 1; } int version; if (!ITGetVersion(&version)) { if (!install_itsutils()) { fprintf(stderr, "could not install itsutils on the device\n"); res=1; goto exit; } if (!ITGetVersion(&version)) { fprintf(stderr, "could not install correct version of itsutils\n"); res=1; goto exit; } } if (!ITDialNumber(argv[1])) res= 1; exit: CeRapiUninit(); return res; }