Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ DLL VC
Hi,
I have written a code to load a predefined windows WinSCard.dll into the c++ code. but in d process m having exceptions AccessViolationException and NullReferenceException.
Code is:
HINSTANCE hinDLL;
hinDLL = LoadLibrary((LPCWSTR("WinSCard.dll"));
if (hinDLL != 0)
    {
              context=(EstablishContext) GetProcAddress(hinDLL,"SCardEstablishContext");
 
              listreaders=(ListReaders) GetProcAddress(hinDLL,"SCardListReaders");
 
              sconnect=(Connect) GetProcAddress(hinDLL,"SCardConnect");
 
              }
else{
     cout<<"Library failed to load\n";
     }
}
 

But this code is not going into the if part but only into "Library failed to load".
I m using visual c++ 2010 express . I have already placed the dll into the project directory with the .exe file of project.
Posted 21-May-12 4:41am
Comments
Sergey Chepurin at 21-May-12 10:52am
   
It could be UNICODE vs ANSI issue. Try to use TEXT macro -
Windows.h
...
LoadLibrary(TEXT("WinSCard.dll"));
Hellexx at 21-May-12 10:58am
   
i have tried dat too... but to no avail...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If the call to LoadLibrary()[^] fails, then you should check the system error returned by GetLastError().
  Permalink  
Comments
Aescleal at 21-May-12 13:00pm
   
And use FormatMessage to get the text representation. Ironically considering FormatMessage is one of the biggest pains in the bum in the Windows API.
Richard MacCutchan at 21-May-12 13:05pm
   
It does seem so at first, but after a couple of goes I found it quite simple and very useful.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It may be the location of the file. You say you put it in the directory of the .exe, however if you're running in the VS debugger, the "working" directory is normally the directory containing the .sln file.
 
You can set the working directory for debugging in the project properties.
 
Hope this helps.
  Permalink  
Comments
Hellexx at 21-May-12 10:56am
   
i have put appended the path in almost all of the of the directory structure of vb c++..... but still not workin..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

errors are:-
'latch-c++.exe': Loaded 'D:\latch-c++\Debug\latch-c++.exe', Symbols loaded.
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\mscoree.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\secur32.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\msvcr100d.dll', Symbols loaded.
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\msvcp100d.dll', Symbols loaded.
'latch-c++.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\shlwapi.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\user32.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\imm32.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\clr.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\msvcr100_clr0400.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Culture.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\mscorlib\1a275c96b17001a1c2ddf29c5fd0c7fd\mscorlib.ni.dll', No native symbols in symbol file.
'latch-c++.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Symbols loaded.
'latch-c++.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\nlssorting.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\ole32.dll', Symbols loaded (source information stripped).
'latch-c++.exe' (Managed (v4.0.30319)): Loaded 'D:\latch-c++\Debug\latch-c++.exe', Symbols loaded.
'latch-c++.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\clrjit.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\diasymreader.dll', Symbols loaded (source information stripped).
'latch-c++.exe': Loaded 'C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\System\2aa74f395dcc166aa65fb8e72aae7475\System.ni.dll', No native symbols in symbol file.
'latch-c++.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Symbols loaded.
First-chance exception at 0x00000000 in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x791659f0 (clr.dll) in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x791659f0 (clr.dll) in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x791659f0 (clr.dll) in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x791659f0 (clr.dll) in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
A first chance exception of type 'System.AccessViolationException' occurred in latch-c++.exe
First-chance exception at 0x791659f0 (clr.dll) in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x791659f0 (clr.dll) in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x791659f0 (clr.dll) in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x791659f0 (clr.dll) in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x791659f0 (clr.dll) in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
First-chance exception at 0x791659f0 (clr.dll) in latch-c++.exe: 0xC0000005: Access violation reading location 0x00000000.
'latch-c++.exe': Loaded 'C:\WINDOWS\system32\version.dll', Symbols loaded (source information stripped).
An unhandled exception of type 'System.AccessViolationException' occurred in latch-c++.exe
 
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
 
The thread 'Win32 Thread' (0x1720) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x9bc) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x8b4) has exited with code 0 (0x0).
The program '[3728] latch-c++.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
The program '[3728] latch-c++.exe: Native' has exited with code 0 (0x0).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I just noticed
hinDLL = LoadLibrary((LPCWSTR("WinSCard.dll"));
which is obviously wrong. You are casting an ASCII string to Unicode instead of using the L prefix or _T() macro. This means you are (probably) sending garbage to the LoadLibrary() function.
 
Please note casts do not do conversion.
  Permalink  
Comments
Hellexx at 22-May-12 1:56am
   
hey richard,
i m getting a value in hinDLL n context but the next function listreaders is getting an undefined value

context=(EstablishContext) GetProcAddress(hinDLL,"SCardEstablishContext");

listreaders=(ListReaders) GetProcAddress(hinDLL,"SCardListReaders");

sconnect=(Connect) GetProcAddress(hinDLL,"SCardConnect");
Richard MacCutchan at 22-May-12 4:50am
   
What is context defined as? It should be a pointer to a function, but you seem to be casting it to something else. Note: please edit your question and add the extra information there.
Hellexx at 24-May-12 2:30am
   
thanx .. i used the L to convert ascii to unicode and it worked....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Maciej Los 250
3 Shweta N Mishra 216
4 PIEBALDconsult 174
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 21 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100