error C2664

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 2
Reputation: mariaprabu is an unknown quantity at this point 
Solved Threads: 0
mariaprabu mariaprabu is offline Offline
Newbie Poster

error C2664

 
0
  #1
Mar 20th, 2007
Hi,
While compiling error found. Please give a solution as soon as posible.

CODING:
----------
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "delayImp.h"
  5. //
  6. // Local copies of strlen, memcmp, and memcpy to make sure we do not need the CRT
  7. //
  8. extern "C"
  9. static inline size_t
  10. __strlen(const char * sz) {
  11. const char *szEnd = sz;
  12. while( *szEnd++ ) {
  13. ;
  14. }
  15. return szEnd - sz - 1;
  16. }
  17. static inline int
  18. __memcmp(const void * pv1, const void * pv2, size_t cb) {
  19. if (!cb) {
  20. return 0;
  21. }
  22. while ( --cb && *(char *)pv1 == *(char *)pv2 ) {
  23. pv1 = (char *)pv1 + 1;
  24. pv2 = (char *)pv2 + 1;
  25. }
  26. return *((unsigned char *)pv1) - *((unsigned char *)pv2);
  27. }
  28. static inline void *
  29. __memcpy(void * pvDst, const void * pvSrc, size_t cb) {
  30. void * pvRet = pvDst;
  31. //
  32. // copy from lower addresses to higher addresses
  33. //
  34. while (cb--) {
  35. *(char *)pvDst = *(char *)pvSrc;
  36. pvDst = (char *)pvDst + 1;
  37. pvSrc = (char *)pvSrc + 1;
  38. }
  39. return pvRet;
  40. }
  41.  
  42. // utility function for calculating the index of the current import
  43. // for all the tables (INT, BIAT, UIAT, and IAT).
  44. inline unsigned
  45. IndexFromPImgThunkData(PCImgThunkData pitdCur, PCImgThunkData pitdBase) {
  46. return (unsigned) (pitdCur - pitdBase);
  47. }
  48. // C++ template utility function for converting RVAs to pointers
  49. //
  50. #if defined(_M_IA64)
  51. #pragma section(".base", long, read)
  52. extern "C"
  53. __declspec(allocate(".base"))
  54. const IMAGE_DOS_HEADER __ImageBase;
  55. #else
  56. extern "C"
  57. const IMAGE_DOS_HEADER __ImageBase;
  58. #endif
  59. template <class X>
  60. X PFromRva(RVA rva) {
  61. return X(PBYTE(&__ImageBase) + rva);
  62. }
  63. // structure definitions for the list of unload records
  64. typedef struct UnloadInfo * PUnloadInfo;
  65. typedef struct UnloadInfo {
  66. PUnloadInfo puiNext;
  67. PCImgDelayDescr pidd;
  68. } UnloadInfo;
  69. // utility function for calculating the count of imports given the base
  70. // of the IAT. NB: this only works on a valid IAT!
  71. inline unsigned
  72. CountOfImports(PCImgThunkData pitdBase) {
  73. unsigned cRet = 0;
  74. PCImgThunkData pitd = pitdBase;
  75. while (pitd->u1.Function) {
  76. pitd++;
  77. cRet++;
  78. }
  79. return cRet;
  80. }
  81. extern "C"
  82. PUnloadInfo __puiHead = 0;
  83. struct ULI : public UnloadInfo {
  84. ULI(PCImgDelayDescr pidd_) {
  85. pidd = pidd_;
  86. Link();
  87. }
  88. ~ULI() {
  89. Unlink();
  90. }
  91. void *
  92. operator new(size_t cb) {
  93. return ::LocalAlloc(LPTR, cb);
  94. }
  95. void
  96. operator delete(void * pv) {
  97. ::LocalFree(pv);
  98. }
  99. void
  100. Unlink() {
  101. PUnloadInfo * ppui = &__puiHead;
  102. while (*ppui && *ppui != this) {
  103. ppui = &((*ppui)->puiNext);
  104. }
  105. if (*ppui == this) {
  106. *ppui = puiNext;
  107. }
  108. }
  109. void
  110. Link() {
  111. puiNext = __puiHead;
  112. __puiHead = this;
  113. }
  114. };
  115. // For our own internal use, we convert to the old
  116. // format for convenience.
  117. //
  118. struct InternalImgDelayDescr {
  119. DWORD grAttrs; // attributes
  120. LPCSTR szName; // pointer to dll name
  121. HMODULE * phmod; // address of module handle
  122. PImgThunkData pIAT; // address of the IAT
  123. PCImgThunkData pINT; // address of the INT
  124. PCImgThunkData pBoundIAT; // address of the optional bound IAT
  125. PCImgThunkData pUnloadIAT; // address of optional copy of original IAT
  126. DWORD dwTimeStamp; // 0 if not bound,
  127. // O.W. date/time stamp of DLL bound to (Old BIND)
  128. };
  129. typedef InternalImgDelayDescr * PIIDD;
  130. typedef const InternalImgDelayDescr * PCIIDD;
  131. static inline
  132. PIMAGE_NT_HEADERS WINAPI
  133. PinhFromImageBase(HMODULE hmod) {
  134. return PIMAGE_NT_HEADERS(PBYTE(hmod) + PIMAGE_DOS_HEADER(hmod)->e_lfanew);
  135. }
  136. static inline
  137. void WINAPI
  138. OverlayIAT(PImgThunkData pitdDst, PCImgThunkData pitdSrc) {
  139. __memcpy(pitdDst, pitdSrc, CountOfImports(pitdDst) * sizeof IMAGE_THUNK_DATA);
  140. }
  141. static inline
  142. DWORD WINAPI
  143. TimeStampOfImage(PIMAGE_NT_HEADERS pinh) {
  144. return pinh->FileHeader.TimeDateStamp;
  145. }
  146. static inline
  147. bool WINAPI
  148. FLoadedAtPreferredAddress(PIMAGE_NT_HEADERS pinh, HMODULE hmod) {
  149. return UINT_PTR(hmod) == pinh->OptionalHeader.ImageBase;
  150. }
  151.  
  152. // Do the InterlockedExchange magic
  153. //
  154. #ifdef _M_IX86
  155. #undef InterlockedExchangePointer
  156. #define InterlockedExchangePointer(Target, Value) \
  157. (PVOID)(LONG_PTR)InterlockedExchange((PLONG)(Target), (LONG)(LONG_PTR)(Value))
  158. #if (_MSC_VER >= 1300)
  159. typedef __w64 unsigned long *PULONG_PTR;
  160. #else
  161. typedef unsigned long *PULONG_PTR;
  162. #endif
  163. #endif
  164. extern "C"
  165. FARPROC WINAPI
  166. __delayLoadHelper2(
  167. PCImgDelayDescr pidd,
  168. FARPROC * ppfnIATEntry
  169. ) {
  170. // Set up some data we use for the hook procs but also useful for
  171. // our own use
  172. //
  173. InternalImgDelayDescr idd = {
  174. pidd->grAttrs,
  175. PFromRva<LPCSTR>(pidd->rvaDLLName),
  176. PFromRva<HMODULE*>(pidd->rvaHmod),
  177. PFromRva<PImgThunkData>(pidd->rvaIAT),
  178. PFromRva<PCImgThunkData>(pidd->rvaINT),
  179. PFromRva<PCImgThunkData>(pidd->rvaBoundIAT),
  180. PFromRva<PCImgThunkData>(pidd->rvaUnloadIAT),
  181. pidd->dwTimeStamp
  182. };
  183. DelayLoadInfo dli = {
  184. sizeof DelayLoadInfo,
  185. pidd,
  186. ppfnIATEntry,
  187. idd.szName,
  188. { 0 },
  189. 0,
  190. 0,
  191. 0
  192. };
  193. if (0 == (idd.grAttrs & dlattrRva)) {
  194. PDelayLoadInfo rgpdli[1] = { &dli };
  195. RaiseException(
  196. VcppException(ERROR_SEVERITY_ERROR, ERROR_INVALID_PARAMETER),
  197. 0,
  198. 1,
  199. PULONG_PTR(rgpdli)
  200. );
  201. return 0;
  202. }
  203. HMODULE hmod = *idd.phmod;
  204. // Calculate the index for the IAT entry in the import address table
  205. // N.B. The INT entries are ordered the same as the IAT entries so
  206. // the calculation can be done on the IAT side.
  207. //
  208. const unsigned iIAT = IndexFromPImgThunkData(PCImgThunkData(ppfnIATEntry), idd.pIAT);
  209. const unsigned iINT = iIAT;
  210. PCImgThunkData pitd = &(idd.pINT[iINT]);
  211. dli.dlp.fImportByName = !IMAGE_SNAP_BY_ORDINAL(pitd->u1.Ordinal);
  212. if (dli.dlp.fImportByName) {
  213. dli.dlp.szProcName = LPCSTR(PFromRva<PIMAGE_IMPORT_BY_NAME>(RVA(UINT_PTR(pitd->u1.AddressOfData)))->Name);
  214. }
  215. else {
  216. dli.dlp.dwOrdinal = DWORD(IMAGE_ORDINAL(pitd->u1.Ordinal));
  217. }
  218. // Call the initial hook. If it exists and returns a function pointer,
  219. // abort the rest of the processing and just return it for the call.
  220. //
  221. FARPROC pfnRet = NULL;
  222. if (__pfnDliNotifyHook2) {
  223. pfnRet = ((*__pfnDliNotifyHook2)(dliStartProcessing, &dli));
  224. if (pfnRet != NULL) {
  225. goto HookBypass;
  226. }
  227. }
  228. // Check to see if we need to try to load the library.
  229. //
  230. if (hmod == 0) {
  231. if (__pfnDliNotifyHook2) {
  232. hmod = HMODULE(((*__pfnDliNotifyHook2)(dliNotePreLoadLibrary, &dli)));
  233. }
  234. if (hmod == 0) {
  235. hmod = ::LoadLibrary(dli.szDll);
  236. }
  237. if (hmod == 0) {
  238. dli.dwLastError = ::GetLastError();
  239. if (__pfnDliFailureHook2) {
  240. // when the hook is called on LoadLibrary failure, it will
  241. // return 0 for failure and an hmod for the lib if it fixed
  242. // the problem.
  243. //
  244. hmod = HMODULE((*__pfnDliFailureHook2)(dliFailLoadLib, &dli));
  245. }
  246. if (hmod == 0) {
  247. PDelayLoadInfo rgpdli[1] = { &dli };
  248. RaiseException(
  249. VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND),
  250. 0,
  251. 1,
  252. PULONG_PTR(rgpdli)
  253. );
  254.  
  255. // If we get to here, we blindly assume that the handler of the exception
  256. // has magically fixed everything up and left the function pointer in
  257. // dli.pfnCur.
  258. //
  259. return dli.pfnCur;
  260. }
  261. }
  262. // Store the library handle. If it is already there, we infer
  263. // that another thread got there first, and we need to do a
  264. // FreeLibrary() to reduce the refcount
  265. //
  266. HMODULE hmodT = HMODULE(InterlockedExchangePointer((PVOID *) idd.phmod, PVOID(hmod)));
  267. if (hmodT != hmod) {
  268. // add lib to unload list if we have unload data
  269. if (pidd->rvaUnloadIAT) {
  270. // suppress prefast warning 6014, the object is saved in a link list in the constructor of ULI
  271. #pragma warning(suppress:6014)
  272. new ULI(pidd);
  273. }
  274. }
  275. else {
  276. ::FreeLibrary(hmod);
  277. }
  278.  
  279. }
  280. // Go for the procedure now.
  281. //
  282. dli.hmodCur = hmod;
  283. if (__pfnDliNotifyHook2) {
  284. pfnRet = (*__pfnDliNotifyHook2)(dliNotePreGetProcAddress, &dli);
  285. }
  286. if (pfnRet == 0) {
  287. if (pidd->rvaBoundIAT && pidd->dwTimeStamp) {
  288. // bound imports exist...check the timestamp from the target image
  289. //
  290. PIMAGE_NT_HEADERS pinh(PinhFromImageBase(hmod));
  291. if (pinh->Signature == IMAGE_NT_SIGNATURE &&
  292. TimeStampOfImage(pinh) == idd.dwTimeStamp &&
  293. FLoadedAtPreferredAddress(pinh, hmod)) {
  294. // Everything is good to go, if we have a decent address
  295. // in the bound IAT!
  296. //
  297. pfnRet = FARPROC(UINT_PTR(idd.pBoundIAT[iIAT].u1.Function));
  298. if (pfnRet != 0) {
  299. goto SetEntryHookBypass;
  300. }
  301. }
  302. }
  303. pfnRet = ::GetProcAddress(hmod, dli.dlp.szProcName);
  304. }
  305. if (pfnRet == 0) {
  306. dli.dwLastError = ::GetLastError();
  307. if (__pfnDliFailureHook2) {
  308. // when the hook is called on GetProcAddress failure, it will
  309. // return 0 on failure and a valid proc address on success
  310. //
  311. pfnRet = (*__pfnDliFailureHook2)(dliFailGetProc, &dli);
  312. }
  313. if (pfnRet == 0) {
  314. PDelayLoadInfo rgpdli[1] = { &dli };
  315. RaiseException(
  316. VcppException(ERROR_SEVERITY_ERROR, ERROR_PROC_NOT_FOUND),
  317. 0,
  318. 1,
  319. PULONG_PTR(rgpdli)
  320. );
  321. // If we get to here, we blindly assume that the handler of the exception
  322. // has magically fixed everything up and left the function pointer in
  323. // dli.pfnCur.
  324. //
  325. pfnRet = dli.pfnCur;
  326. }
  327. }
  328. SetEntryHookBypass:
  329. *ppfnIATEntry = pfnRet;
  330. HookBypass:
  331. if (__pfnDliNotifyHook2) {
  332. dli.dwLastError = 0;
  333. dli.hmodCur = hmod;
  334. dli.pfnCur = pfnRet;
  335. (*__pfnDliNotifyHook2)(dliNoteEndProcessing, &dli);
  336. }
  337. return pfnRet;
  338. }
  339. extern "C"
  340. BOOL WINAPI
  341. __FUnloadDelayLoadedDLL2(LPCSTR szDll) {
  342.  
  343. BOOL fRet = FALSE;
  344. PUnloadInfo pui = __puiHead;
  345.  
  346. for (pui = __puiHead; pui; pui = pui->puiNext) {
  347. LPCSTR szName = PFromRva<LPCSTR>(pui->pidd->rvaDLLName);
  348. size_t cbName = __strlen(szName);
  349. // Intentionally case sensitive to avoid complication of using the CRT
  350. // for those that don't use the CRT...the user can replace this with
  351. // a variant of a case insenstive comparison routine
  352. //
  353. if (cbName == __strlen(szDll) && __memcmp(szDll, szName, cbName) == 0) {
  354. break;
  355. }
  356. }
  357. if (pui && pui->pidd->rvaUnloadIAT) {
  358. PCImgDelayDescr pidd = pui->pidd;
  359. HMODULE * phmod = PFromRva<HMODULE*>(pidd->rvaHmod);
  360. HMODULE hmod = *phmod;
  361. OverlayIAT(
  362. PFromRva<PImgThunkData>(pidd->rvaIAT),
  363. PFromRva<PCImgThunkData>(pidd->rvaUnloadIAT)
  364. );
  365. ::FreeLibrary(hmod);
  366. *phmod = NULL;
  367.  
  368. delete reinterpret_cast<ULI*> (pui);
  369. fRet = TRUE;
  370. }
  371. return fRet;
  372. }
  373. extern "C"
  374. HRESULT WINAPI
  375. __HrLoadAllImportsForDll(LPCSTR szDll) {
  376. HRESULT hrRet = HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND);
  377. PIMAGE_NT_HEADERS pinh = PinhFromImageBase(HMODULE(&__ImageBase));
  378. // Scan the Delay load IAT/INT for the dll in question
  379. //
  380. if (pinh->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size) {
  381. PCImgDelayDescr pidd;
  382. pidd = PFromRva<PCImgDelayDescr>(
  383. pinh->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress
  384. );
  385. // Check all of the dlls listed up to the NULL one.
  386. //
  387. while (pidd->rvaDLLName) {
  388. // Check to see if it is the DLL we want to load.
  389. // Intentionally case sensitive to avoid complication of using the CRT
  390. // for those that don't use the CRT...the user can replace this with
  391. // a variant of a case insenstive comparison routine.
  392. //
  393. LPCSTR szDllCur = PFromRva<LPCSTR>(pidd->rvaDLLName);
  394. size_t cchDllCur = __strlen(szDllCur);
  395. if (cchDllCur == __strlen(szDll) && __memcmp(szDll, szDllCur, cchDllCur) == 0) {
  396. // We found it, so break out with pidd and szDllCur set appropriately
  397. //
  398. break;
  399. }
  400.  
  401. // Advance to the next delay import descriptor
  402. //
  403. pidd++;
  404. }
  405.  
  406. if (pidd->rvaDLLName) {
  407. // Found a matching DLL name, now process it.
  408. //
  409. // Set up the internal structure
  410. //
  411. FARPROC * ppfnIATEntry = PFromRva<FARPROC*>(pidd->rvaIAT);
  412. size_t cpfnIATEntries = CountOfImports(PCImgThunkData(ppfnIATEntry));
  413. FARPROC * ppfnIATEntryMax = ppfnIATEntry + cpfnIATEntries;
  414. for (;ppfnIATEntry < ppfnIATEntryMax; ppfnIATEntry++) {
  415. __delayLoadHelper2(pidd, ppfnIATEntry);
  416. }
  417. // Done, indicate some semblance of success
  418. //
  419. hrRet = S_OK;
  420. }
  421. }
  422. return hrRet;
  423. }
OUTPUT:
----------

1>------ Build started: Project: vc, Configuration: Debug Win32 ------
1>Compiling...
1>delayhlp.cpp
1>c:\l2r\include\delayhlp.cpp(308) : error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'LPCSTR' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>vc - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped

Thanks,
Maria Prabudass E.
Last edited by Ancient Dragon; Mar 20th, 2007 at 8:31 am. Reason: missing code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: error C2664

 
0
  #2
Mar 20th, 2007
you are compiling your program with UNICODE set. Turn UNICODE off by selecting Project --> Properties, Configuration Properties, General, then set the "Character Set" to "Not Set"
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: mariaprabu is an unknown quantity at this point 
Solved Threads: 0
mariaprabu mariaprabu is offline Offline
Newbie Poster

Re: error C2664

 
0
  #3
Mar 21st, 2007
Thanks

Regards,
Mariaprabudass
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC