C # Call C Functions

Source: Internet
Author: User
C code:
# Include <stdio. h>
# Include <assert. h>
# Define MAXLEN 1000
Int getline (char [], int );

// For C # Call _ declspec
_ Declspec (dllexport) void reverseline (char dest [], const char source []);

Int main (int argc, char * argv [])
{
Char currentline [MAXLEN];
Int maxcount = 0;
Int currentlen = 0;
Char resultline [MAXLEN];
While (currentlen = getline (currentline, MAXLEN ))
{
Reverseline (resultline, currentline );
Printf ("\ n % s \ n", resultline );
}

Return 0;
}
Int getline (char line [], int len)
{
// Printf ("------- start getline ----------------------- \ n ");
Char c;
Int I = 0;
For (; (I <len & (c = getchar ())! = EOF & (c! = '\ N'); I ++)
{

Line [I] = c;
// Printf ("line [% d] = % c", I, c );
}

Line [I] = '\ 0 ';
// Printf ("\ n line [% d] = null \ n", I + 1 );

// Printf ("------- end getline ----------------------- \ n ");
Return I;
}

Void reverseline (char dest [], const char source [])
{

Int I = 0;
Int len = 0;
For (I = 0; I <MAXLEN; I ++)
{
If (source [I] = '\ 0 ')
{
Len = I;
Break;
}
// Dest [I] = source [I];
}

// Printf ("\ nlen = % d \ n", len );

For (I = 0; I <len; I ++)
{
Dest [len-1-i] = source [I];
// Printf ("dest [% d] = % c", len-1-i, dest [len-1-i]);
}

Dest [len] = '\ 0 ';

}

Use MS cl.exe to generate dll

Cl.exe reverseline. c/LD
Note that/LD is in uppercase. If it is written as/ld, it is directly ignored by cl.

Compilation output written normally:
Reverseline. c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.

/Out: reverseline. dll
/Dll
/Implib: reverseline. lib
Reverseline. obj
Creating library reverseline. lib and object reverseline. exp

The export function does not add _ declspec (dllexport) to compile the output:
Reverseline. c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.

/Out: reverseline. dll
/Dll
/Implib: reverseline. lib
Reverseline. obj

C # code: Copy reverseline. dll to the. exe directory generated by C #.
C # code:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Runtime. InteropServices;

Namespace CallCLib
{
Class CLib
{
[DllImport ("reverseline. dll")]
Public static extern void reverseline (StringBuilder dest, StringBuilder surce );
}
Class Program
{
Static void Main (string [] args)
{
// Char [] a = {'1', '2', '3 '};
// Char [] B = new char [] {};
StringBuilder a = new StringBuilder ("123 ");
StringBuilder B = new StringBuilder ();
CLib. reverseline (B, );

Console. WriteLine ("a = {0} B = {1}", a, B );

Console. Read ();
}
}
}
Result generated after running:
A: 123 B = 321

Problem:
(1) If you call
Public static extern void reverseline (char [] dest, char [] surce );

Char [] a = {'1', '2', '3 '};
Char [] B = new char [] {};

CLib. reverseline (B, );
Exception:
System. AccessViolationException not processed
Message = "try to read or write the protected memory. This usually indicates that other memory is damaged. "
Source = "CallCLib"
StackTrace:
In CallCLib. CLib. reverseline (Char [] dest, Char [] surce)
In the CallCLib. Program. Main (String [] args) Position E: \ lhb \ Program \ CallCLib \ Program. cs: row 23
In System. AppDomain. _ nExecuteAssembly (Assembly assembly, String [] args)
In System. AppDomain. ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String [] args)
In Microsoft. VisualStudio. HostingProcess. HostProc. RunUsersAssembly ()
In System. Threading. ThreadHelper. ThreadStart_Context (Object state)
In System. Threading. ExecutionContext. Run (ExecutionContext executionContext, ContextCallback callback, Object state)
In System. Threading. ThreadHelper. ThreadStart ()
InnerException:
(2) If you call
Public static extern void reverseline (char [] dest, char [] surce );

String a = "123 ";
String B = "";

CLib. reverseline (B, );

Result generated after running:
A = 123 B =

(2) If the export function in c does not add _ declspec (dllexport), it is directly void reverseline (char dest [], const char source []);
Exception:
System. EntryPointNotFoundException not processed
Message = "unable to find the entry point named" reverseline "in DLL" reverseline. dll. "
Source = "CallCLib"
TypeName = ""
StackTrace:
In CallCLib. CLib. reverseline (Char [] dest, Char [] surce)
In the CallCLib. Program. Main (String [] args) Position E: \ lhb \ Program \ CallCLib \ Program. cs: row 23
In System. AppDomain. _ nExecuteAssembly (Assembly assembly, String [] args)
In System. AppDomain. ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String [] args)
In Microsoft. VisualStudio. HostingProcess. HostProc. RunUsersAssembly ()
In System. Threading. ThreadHelper. ThreadStart_Context (Object state)
In System. Threading. ExecutionContext. Run (ExecutionContext executionContext, ContextCallback callback, Object state)
In System. Threading. ThreadHelper. ThreadStart ()
InnerException:

Q: Is it a function written in the c standard library that cannot be called by. net without modifying the c source code (such as adding _ declspec or defining the def file?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.