Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

.net - calling c function from C#

as a simple example, i have a C function which receives an int,increment it and return incremented value.on the other side I enter the input number to textbox in C#. how can i call C function to process the number and return it back from c#?Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are looking for P/Invoke.

You do will need a Reference to System.Runtime.InteropServices and then do the following, if your C DLL contains a function called increase_int:

[DllImport("mylib.dll")]
private static extern int increase_int(int in_value);

and use it from your code doing

int newValue = increase_int(oldValue);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...