Click here to Skip to main content
15,867,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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
Comments
Sergey Chepurin 21-May-12 10:52am    
It could be UNICODE vs ANSI issue. Try to use TEXT macro -
Windows.h
...
LoadLibrary(TEXT("WinSCard.dll"));
Anil Kumar 23 21-May-12 10:58am    
i have tried dat too... but to no avail...

If the call to LoadLibrary()[^] fails, then you should check the system error returned by GetLastError().
 
Share this answer
 
Comments
Aescleal 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 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.
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.
 
Share this answer
 
Comments
Anil Kumar 23 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..
errors are:-
CSS
'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).
 
Share this answer
 
I just noticed
C++
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.
 
Share this answer
 
Comments
Anil Kumar 23 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 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.
Anil Kumar 23 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900