0

Here is my question..

I created a c language console application to read and write NFC card. In there, there are 2 functions for read and write.

long ReadCard(char * cID)
{
// code here 
}

long WriteCard(char * cID)
{
// code here
}

to call this function , i used this code..

for read card..

char * cID = malloc(17 * sizeof(char));
long _ret = ReadCard(cID);

for write card..

char CID[17];
printf("Enter Card ID \n");
scanf("%s", CID);
long _ret = WriteCard(CID);

this is perfectly working...

then i created a dll using above console application and tried to use it in a c# application..

In c# code first i import dll and function declared like this..

[DllImport("NFC_Driver_DLL.dll", CallingConvention =   CallingConvention.Cdecl)]
public static extern int ReadCard([Out] char[] cID);

[DllImport("NFC_Driver_DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int WriteCard([In] char[] cID);

after that , in order to read card, I used this code..

char[] CardID = new char[17];
int a = ReadCard(CardID);
string s = new string(CardID);
MessageBox.Show(s);

this is fully tested and 100% working..

but i don't know, how to call write card function from c# side..

i tried this code...

char[] CardID = new char[17] ;
int a = WriteCard(CardID);

when executing this code, it occurs a exception which tell this message..

An unhandled exception of type 'System.EntryPointNotFoundException' occurred in WpfNFCTest.exe

Additional information: Unable to find an entry point named 'WriteCard' in DLL 'NFC_Driver_DLL.dll'.

please help me to call this write function please...

  • 1
    Try wrapping c function declarations with: extern "C" { } – Radon Oct 05 '16 at 08:57
  • can you please explain more?? – Dilan Sarith Oct 05 '16 at 09:01
  • 1
    Declare c functions like this: extern "C" { long ReadCard(char * cID); long WriteCard(char * cID); } . This should be done only if __cplusplus is defined. Because cpp function names are exported differently so your c# app cant find the enrty point. – Radon Oct 05 '16 at 09:08
  • but..this is completely c library...there is no cpp file in here – Dilan Sarith Oct 05 '16 at 09:23
  • Try using dependency walker on your library to check how your function names are exported. http://www.dependencywalker.com . Just Drag and drop your dll into it. – Radon Oct 05 '16 at 09:35
  • http://stackoverflow.com/questions/10799685/how-to-pass-strings-from-c-sharp-to-c-and-from-c-to-c-using-dllimport may help – Paul Baxter Mar 08 '17 at 20:34
  • @Dilan even if this is straight C it needs to be declared extern or in a .def file – Paul Baxter Apr 03 '17 at 17:03

0 Answers0