请问高手,可以把C语言嵌入到C#中吗

Grace_ghb 2007-01-04 09:29:52
因为用C来开发UI我不太会,但是应用有一段又必须用C,
所以有此一问

或者能在C#中引用C的头文件嘛

谢谢大家的回答
...全文
1184 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
julong88 2007-01-04
  • 打赏
  • 举报
回复
可以用C写“标准Dll”,用C#去调用。。
superxiaomm 2007-01-04
  • 打赏
  • 举报
回复
不可以的,只能把c的代码封装起来,用__dllexport出来.在c#里面象调用win api一样的调用.导出的时候最好def文件里面写好导出的函数名.
c#里面用[DllImport()] public static extern xxx(xxx);
北京的雾霾天 2007-01-04
  • 打赏
  • 举报
回复
C#里是可以嵌入非安全代码的,就是unsafe编程,如果有unsafe编程则要在编译器上加上unsafe的选项.
Grace_ghb 2007-01-04
  • 打赏
  • 举报
回复
头文件能封成dll? 不太会
honkerhero 2007-01-04
  • 打赏
  • 举报
回复
PS:封成DLL只要有入口参数就不用dllimport
用regsvr32注册一下就ok了
honkerhero 2007-01-04
  • 打赏
  • 举报
回复
封成DLL
Grace_ghb 2007-01-04
  • 打赏
  • 举报
回复
上面的好像都是都用到了dllimport,
呵呵,我是要使用头文件。。。
burn3tt 2007-01-04
  • 打赏
  • 举报
回复
msdn unsafe~
fencole 2007-01-04
  • 打赏
  • 举报
回复
unsafe
liujia_0421 2007-01-04
  • 打赏
  • 举报
回复
try..

unsafe
flyrain000 2007-01-04
  • 打赏
  • 举报
回复
使用unsafe编程,如下例
public unsafe class Dog
{

public uint DogBytes, DogAddr;
public byte[] DogData;
public uint Retcode;


[DllImport("Win32dllRead.dll", CharSet = CharSet.Ansi)]
public static unsafe extern uint DogRead(uint idogBytes, uint idogAddr, byte* pdogData);
[DllImport("Win32dll.dll", CharSet = CharSet.Ansi)]
public static unsafe extern uint DogWrite(uint idogBytes, uint idogAddr, byte* pdogData);

public unsafe Dog(ushort num)
{
DogBytes = num;
DogData = new byte[DogBytes];
}
public unsafe void ReadDog()
{
fixed (byte* pDogData = &DogData[0])
{
Retcode = DogRead(DogBytes, DogAddr, pDogData);
}
}
}
Red_angelX 2007-01-04
  • 打赏
  • 举报
回复
// fastcopy.cs
// compile with: /unsafe
using System;

class Test
{
// The unsafe keyword allows pointers to be used within
// the following method:
static unsafe void Copy(byte[] src, int srcIndex,
byte[] dst, int dstIndex, int count)
{
if (src == null || srcIndex < 0 ||
dst == null || dstIndex < 0 || count < 0)
{
throw new ArgumentException();
}
int srcLen = src.Length;
int dstLen = dst.Length;
if (srcLen - srcIndex < count ||
dstLen - dstIndex < count)
{
throw new ArgumentException();
}


// The following fixed statement pins the location of
// the src and dst objects in memory so that they will
// not be moved by garbage collection.
fixed (byte* pSrc = src, pDst = dst)
{
byte* ps = pSrc;
byte* pd = pDst;

// Loop over the count in blocks of 4 bytes, copying an
// integer (4 bytes) at a time:
for (int n =0 ; n < count/4 ; n++)
{
*((int*)pd) = *((int*)ps);
pd += 4;
ps += 4;
}

// Complete the copy by moving any bytes that weren't
// moved in blocks of 4:
for (int n =0; n < count%4; n++)
{
*pd = *ps;
pd++;
ps++;
}
}
}


static void Main(string[] args)
{
byte[] a = new byte[100];
byte[] b = new byte[100];
for(int i=0; i<100; ++i)
a[i] = (byte)i;
Copy(a, 0, b, 0, 100);
Console.WriteLine("The first 10 elements are:");
for(int i=0; i<10; ++i)
Console.Write(b[i] + " ");
Console.WriteLine("\n");
}
}

看看这个exp吧 unsafe
Grace_ghb 2007-01-04
  • 打赏
  • 举报
回复
ms说C#借鉴了java的思想,可是这个怎么不行呢,java可以嵌入c的
ilove8 2007-01-04
  • 打赏
  • 举报
回复
不错的想法
Grace_ghb 2007-01-04
  • 打赏
  • 举报
回复
是这样的,我有一个头文件里有很多的结构定义,
我不想手动转换到C#里面,因为设备提供商有可能改变,

我想能不能在C#里面引用这个头文件
Red_angelX 2007-01-04
  • 打赏
  • 举报
回复
unsafe编程
LotusUnix 2007-01-04
  • 打赏
  • 举报
回复
这是一个c#调用c++的看对你有没有帮助
使用命名空间:
using System.Runtime.InteropServices;
using System.Text;

///////////////////////////////////////////////////////////////////
把dll放到相应的bin目录下

声明dll:
[DllImport("msDll.dll")]
public static extern int CalculateStr(StringBuilder szExp,out double pfOut,StringBuilder szErr,int nErrLen);

使用方法:
StringBuilder strT1=new StringBuilder(100);
double fResult=0;
int nRet;
StringBuilder szErr =new StringBuilder(30);
strT1.Append("1/sin90");

nRet=CalculateStr(strT1,out fResult,szErr,30);
if(nRet==1)
{
Response.Write(fResult);
}
else
{
Response.Write(szErr);
}

/////////////////////////////////////////////////////
qpl007 2007-01-04
  • 打赏
  • 举报
回复
“嵌入”肯定是不行的了。。。

你可以用C写“标准Dll”,用C#去调用。。
Grace_ghb 2007-01-04
  • 打赏
  • 举报
回复
怎么没人啊
woanon 2007-01-04
  • 打赏
  • 举报
回复
mark
加载更多回复(3)

110,553

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧