网上看了一些都是调用c++写函数能不能实现c#使用c++写的类各位高手请赐教
解决方案 »
类的有一个成员函数,是返回这个类的指针,那么在C#里面就是一个INTPTR,它是什么你不用管。然后C++文件里面再定义几个函数,专门用于处理这个类的对像的操作,参数就是类对像的指针。这样你在C#层调用C++的这个函数,并将准备好的类对像的指针传回去就好了。这种方法,安全,方便,而且……你那些查到的使用C++函数的方法,就可以用得到了。当然了,暴露出整个C++类的定义也是可以的,不过那样要进行一次全结构定义复制,涉及到类型转换,很麻烦,而且不安全,最好不要这么做。
  
再用C++写一个DLL,在这个DLL中导入另一个DLL的导出类,再定义一些导出函数给C#调用。
  
能给出具体的方案,附代码,给满分
可以发到邮箱
henry3695@hotmail.com
抽象的回复,得0分
  
LZ,奇迹来了,给分吧。。呵呵贴上自己写过的东西,主要内容包含:在C++创建DLL,以及使用C++或者C#分别调用DLL中的方法,环境VS2005。VS2005中很多工程都可以生成DLL,例如atl,mfc,win32等等。选择Win32,步骤如下:
1:新建项目TestDLL,选择win32中的win32控制台应用程序,在“应用程序设置”中,选择“应用程序类型”为DLL,并将下方的附加选项勾上“空项目”,就可以了。 2:添加一个C++类,这时vs2005会生成TestDLL.h和TestDLL.cpp的文件,在.h文件中,键入如下代码:
#define LIBEXPORT_API extern "C" __declspec(dllexport) 
LIBEXPORT_API int f( char * ch);//这是一个测试程序 3:然后在.cpp文件中,必须加入DllMain函数以作为程序出口,并实现函数f:
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{//程序出口
    return TRUE;
} //函数的实现在这里
LIBEXPORT_API int f(char * ch)
{
    return 88;
} 4:编译生成,就能得到与项目同名的TestDLL.dll。注意,生成的dll文件在外面的一个debug文件中。接下来该怎么调用dll,分别在C++和C#做了调用。调用过程如下:
C++调用:
1:首先将dll文件加入工程,拷贝dll置于c++项目文件所在的目录。
2:调用代码
 typedef int (*TEST) (char * ch);//定义调用DLL函数的类型
 //下面是调用过程
 HINSTANCE hDLL; 
 TEST f;
 hDLL = LoadLibrary(_T("TestDLL.dll"));//加载动态链接库TestDLL.dll文件; 
 f = (TEST)GetProcAddress(hDLL,(LPCSTR)"f"); //调用的f函数
 int si ;
 si = f("abc");
  FreeLibrary(hDLL);//卸载TestDLL.dll文件;
  cout<<si;
 return 0;
3:结果——屏幕返回88,正确地调用了dll中的方法。 C#调用:
1:将dll文件拷贝至C#执行文件所在目录,一般在/bin/debug目录下;
2:调用代码
 using System.Runtime.InteropServices;
 ... 
 ...
    class CallDLL
    {//使用一个类封装所有dll的函数
        [DllImport("TestDLL.dll", EntryPoint = "f",
            CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]//指定dll访问入口
        public static extern int  f(string s);//定义调用dll中函数的类型
    }
...
//使用dll中的函数
int m = CallDLL.f("mmm");
3:结果m的值正是88,操作成功。
  
方法都可以调用了,类的定义道理是一样的
在C++中这样定义你的类
class LIBEXPORT_API  YourClass
{
.....
}
  
       
 添加引用 [DllImport("XXXX.dll", CharSet = CharSet.Auto)] ''动态链接库
  public static extern int XXXX(int XXXX);   ''具体方法声明
  
这个问题就当做CSDN本年的公关项目吧期限还有半年
  
c#调用 c++的dll文件http://blog.csdn.net/zhangzxy161723/archive/2009/04/28/4132853.aspx
  
http://topic.csdn.net/u/20090430/17/5148f3de-9f7d-45b1-b5ba-2fcaf06baafc.html
  
MARK,楼主可以搜一下C#调用ActiveDesktop的代码,据说那是个C++类,贴一点,仅供参考,内容过长,分多贴回了这个是根据C++头文件搞的一些枚举
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;namespace HKH.Common.System.ComImports
{
#region ActiveDesktop Enums //Defined in shlobj.h // Flags for IActiveDesktop::GetWallpaperOptions()
//           IActiveDesktop::SetWallpaperOptions()
public enum WallpaperStyle
{
Center = 0,
Tile = 1,
Stretch = 2,
Max = 3
} [Flags]
internal enum ItemState
{
Normal = 0x00000001,
FullScreen = 00000002,
Split = 0x00000004,
ValidSizeStateBits = Normal | Split | FullScreen, // The set of IS_* state bits which define the "size" of the component - these bits are mutually exclusive.
ValidStateBits = Normal | Split | FullScreen | unchecked((int)0x80000000) | 0x40000000 // All of the currently defined IS_* bits.
} internal enum ComponentType
{
HtmlDoc = 0,
Picture = 1,
WebSite = 2,
Control = 3,
CFHtml = 4,
Max = 4
} //Flags for IActiveDesktop::AddDesktopItemWithUI()
internal enum DeskTopItemAddWithUI
{
Default = 0x00000000,
DisplaySubWizard = 0x00000001,
PositionItem = 0x00000002,
} // Flags for IActiveDesktop::ApplyChanges()
[Flags]
internal enum ActiveDesktopApply
{
Save = 0x00000001,
HtmlGen = 0x00000002,
Refresh = 0x00000004,
All = Save | HtmlGen | Refresh,
Force = 0x00000008,
BufferedReferesh = 0x00000010,
DynamicReferesh = 0x00000020
} // Flags for IActiveDesktop::ModifyComponent()
[Flags]
internal enum ComponentElement
{
Type = 0x00000001,
Checked = 0x00000002,
Dirty = 0x00000004,
NoScroll = 0x00000008,
Left = 0x00000010,
Top = 0x00000020,
Width = 0x00000040,
Height = 0x00000080,
ZIndex = 0x00000100,
Source = 0x00000200,
FriendlyName = 0x00000400,
SubscribedUrl = 0x00000800,
OriginalCSI = 0x00001000,
RestoredCSI = 0x00002000,
CurrentItemState = 0x00004000,
All = Type | Checked | Dirty | NoScroll | Left | Width | Height | ZIndex | Source | FriendlyName | Top |
SubscribedUrl | OriginalCSI | RestoredCSI | CurrentItemState
} // Flags for IActiveDesktop::AddUrl()
[Flags]
internal enum AddUrl
{
Silent = 0x0001
} #endregion
}

这个是另个一些需要的结构
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;namespace HKH.Common.System.ComImports
{
#region ActiveDesktop Structs //Defined in shlobj.h internal struct WallpaperOption
{
public int Size; // size of this Structure. 8 = Marshal.SizeOf(WallpaperOption)
public WallpaperStyle Style; // WPSTYLE_* mentioned above
} internal struct ComponentOption
{
public int Size; //Size of this structure, 12 = Marshal.SizeOf(ComponentOption) [MarshalAs(UnmanagedType.Bool)]
public bool EnableComponents; //Enable components? [MarshalAs(UnmanagedType.Bool)]
public bool EnableActiveDesktop; // Active desktop enabled ?
} internal struct ComponentPosition
{
public const int COMPONENT_TOP = 0x3FFFFFFF;
public const int COMPONENT_DEFAULT_LEFT = 0xFFFF;
public const int COMPONENT_DEFAULT_TOP = 0xFFFF; public int Size; //Size of this structure
public int Left; //Left of top-left corner in screen co-ordinates.
public int Top; //Top of top-left corner in screen co-ordinates.
public int Width; // Width in pixels.
public int Height; // Height in pixels.
public int ZIndex; // Indicates the Z-order of the component. [MarshalAs(UnmanagedType.Bool)]
public bool CanResize; // Is the component resizeable? [MarshalAs(UnmanagedType.Bool)]
public bool CanResizeX; // Resizeable in X-direction? [MarshalAs(UnmanagedType.Bool)]
public bool CanResizeY; // Resizeable in Y-direction? public int PreferredLeftPercent; //Left of top-left corner as percent of screen width
public int PreferredTopPercent; //Top of top-left corner as percent of screen height
} internal struct ComponentStateInfo
{
public int Size; // Size of this structure.
public int Left; // Left of the top-left corner in screen co-ordinates.
public int Top; // Top of top-left corner in screen co-ordinates.
public int Width; // Width in pixels.
public int Height; // Height in pixels.
public ItemState ItemState; // State of the component (full-screen mode or split-screen or normal state.
} [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct Component
{
private const int INTERNET_MAX_URL_LENGTH = 2084; // = public int Size; //Size of this structure
public int ID; //Reserved: Set it always to zero.
public ComponentType iComponentType; //One of COMP_TYPE_* [MarshalAs(UnmanagedType.Bool)]
public bool Checked; // Is this component enabled? [MarshalAs(UnmanagedType.Bool)]
public bool Dirty; // Had the component been modified and not yet saved to disk? [MarshalAs(UnmanagedType.Bool)]
public bool NoScroll; // Is the component scrollable? public ComponentPosition Position; // Width, height etc., [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string FriendlyName; // Friendly name of component. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = INTERNET_MAX_URL_LENGTH)]
public string Source; //URL of the component. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = INTERNET_MAX_URL_LENGTH)]
public string SubscribedURL; //Subscrined URL //New fields are added below. Everything above here must exactly match the IE4COMPONENT Structure.
public ItemState ItemState; // Current state of the Component.
public ComponentStateInfo OriginalCSI; // Original state of the component when it was first added.
public ComponentStateInfo RestoredCSI; // Restored state of the component.
} #endregion
}

这个就是我封得C#的类了,最下边是C++的接口到C#的ComImport映射,上边的两个方法是我用的
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;namespace HKH.Common.System.ComImports
{
public class ActiveDesktopUtil
{
private static readonly Guid CLSID_ActiveDesktop = new Guid("{75048700-EF1F-11D0-9888-006097DEACF9}");
private const char WHITESPACE = ' ';
private const string ENDOFCHARARRAY = "\0"; private ActiveDesktopUtil()
{
} private static IActiveDesktop GetActiveDesktop()
{
Type typeActiveDesktop = Type.GetTypeFromCLSID(CLSID_ActiveDesktop);
return (IActiveDesktop)Activator.CreateInstance(typeActiveDesktop);
} /// <summary>
/// Sets Active Desktop Wallpaper
/// </summary>
/// <param name="fileName">the full path of file to set as Wallpaper. If null, reset to default</param>
/// <param name="style"></param>
/// <param name="forceEnabled">indicates whether enable this setting.</param>
public static void SetActiveDesktopWallpaper(string fileName, WallpaperStyle style, bool forceEnabled)
{
IActiveDesktop activeDesktop = GetActiveDesktop();
WallpaperOption wallpaperOption = new WallpaperOption();
ComponentOption componentOption = new ComponentOption(); wallpaperOption.Size = Marshal.SizeOf(wallpaperOption);
wallpaperOption.Style = style;
componentOption.Size = Marshal.SizeOf(componentOption); activeDesktop.GetDesktopItemOptions(ref componentOption, 0); if (!componentOption.EnableActiveDesktop && forceEnabled)
{
componentOption.EnableActiveDesktop = true;
activeDesktop.SetDesktopItemOptions(ref componentOption, 0);
} activeDesktop.SetWallpaperOptions(ref wallpaperOption, 0);
activeDesktop.SetWallpaper(fileName, 0);
activeDesktop.ApplyChanges(ActiveDesktopApply.All);
} /// <summary>
/// Gets Active Desktop Wallpaper
/// </summary>
/// <returns></returns>
public static string GetActiveDesktopWallpaper()
{
IActiveDesktop activeDesktop = GetActiveDesktop();
WallpaperOption wallpaperOption = new WallpaperOption();
wallpaperOption.Size = Marshal.SizeOf(wallpaperOption); string sFile = new string(WHITESPACE, 260);
activeDesktop.GetWallpaper(sFile, 260, 0);
activeDesktop.GetWallpaperOptions(ref wallpaperOption, 0); int pos = sFile.IndexOf(ENDOFCHARARRAY);
if (pos > -1)
{
sFile = sFile.Substring(0, pos);
} return sFile;
}
} #region Internal Com Class Import [ComImport(),Guid("F490EB00-1240-11D1-9888-006097DEACF9"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IActiveDesktop
{
void ApplyChanges(ActiveDesktopApply dwFlags);
void GetWallpaper([MarshalAs(UnmanagedType.LPWStr)]string pwszWallpaper, int cchWallpaper, int dwReserved);
void SetWallpaper([MarshalAs(UnmanagedType.LPWStr)]string pwszWallpaper, int dwReserved);
void GetWallpaperOptions(ref WallpaperOption pwpo, int dwReserved);
void SetWallpaperOptions([In]ref WallpaperOption pwpo, int dwReserved);
void GetPattern([MarshalAs(UnmanagedType.LPWStr)]string pwszPattern, int cchPattern, int dwReserved);
void SetPattern([MarshalAs(UnmanagedType.LPWStr)]string pwszPattern, int dwReserved);
void GetDesktopItemOptions(ref ComponentOption pco, int dwReserved);
void SetDesktopItemOptions([In]ref ComponentOption pco, int dwReserved);
void AddDesktopItem([In]ref Component pcomp, int dwReserved);
void AddDesktopItemWithUI(IntPtr hwnd, [In]ref Component pcomp, DeskTopItemAddWithUI dwFlags);
void ModifyDesktopItem([In]ref Component pcomp, ComponentElement dwFlags);
void RemoveDesktopItem([In]ref Component pcomp, int dwReserved);
void GetDesktopItemCount(out int lpiCount, int dwReserved);
void GetDesktopItem(int nComponent, ref Component pcomp, int dwReserved);
void GetDesktopItemByID(IntPtr dwID, ref Component pcomp, int dwReserved);
void GenerateDesktopItemHtml([MarshalAs(UnmanagedType.LPWStr)]string pwszFileName, [In]ref Component pcomp, int dwReserved);
void AddUrl(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]string pszSource, [In] ref Component pcomp, AddUrl dwFlags);
void GetDesktopItemBySource([MarshalAs(UnmanagedType.LPWStr)]string pwszSource, ref Component pcomp, int dwReserved);
} [ComImport(),Guid("75048700-EF1F-11D0-9888-006097DEACF9")]
internal class ActiveDesktop /* : IActiveDesktop */ 
{
} #endregion
}
应该对你有帮助吧,我在网上搞了半天,才弄出来的
  
不知道楼主的问题解决了没有
我也有楼主相似的问题struct CATInfo
{
  int catCount;
  struct ClsInfo *ClsTable;
  char *dstFolder;
  char *srcFolder;
  int docNum;
  FILE *fpWL;
  int right;
  void*handle;
};请问这个结构怎么转换成C#,主要是FILE这个类,请高手们指点~~~

搜一下FILE这个结构的定义,使用StructLayout对它继续定义
一般来说系统的东西都会在shell.h, user.h,kernal.h的头里,你搜一下就应该能找到记不得下边第一个是不是就是FILE的定义了,以前拿全系统图标的时候用的个东西
[StructLayout(LayoutKind.Sequential)]
internal struct FileInfomation
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
internal enum FileAttributeFlags : int
{
READONLY = 0x00000001,
HIDDEN = 0x00000002,
SYSTEM = 0x00000004,
DIRECTORY = 0x00000010,
ARCHIVE = 0x00000020,
DEVICE = 0x00000040,
NORMAL = 0x00000080,
TEMPORARY = 0x00000100,
SPARSE_FILE = 0x00000200,
REPARSE_POINT = 0x00000400,
COMPRESSED = 0x00000800,
OFFLINE = 0x00001000,
NOT_CONTENT_INDEXED = 0x00002000,
ENCRYPTED = 0x00004000
}
internal enum FileInfoFlags : int
{
ICON = 0x000000100,     // get icon
DISPLAYNAME = 0x000000200,     // get display name
TYPENAME = 0x000000400,     // get type name
ATTRIBUTES = 0x000000800,     // get attributes
ICONLOCATION = 0x000001000,     // get icon location
EXETYPE = 0x000002000,     // return exe type
SYSICONINDEX = 0x000004000,     // get system icon index
LINKOVERLAY = 0x000008000,     // put a link overlay on icon
SELECTED = 0x000010000,     // show icon in selected state
ATTR_SPECIFIED = 0x000020000,     // get only specified attributes
LARGEICON = 0x000000000,     // get large icon
SMALLICON = 0x000000001,     // get small icon
OPENICON = 0x000000002,     // get open icon
SHELLICONSIZE = 0x000000004,     // get shell size icon
PIDL = 0x000000008,     // pszPath is a pidl
USEFILEATTRIBUTES = 0x000000010,     // use passed dwFileAttribute
ADDOVERLAYS = 0x000000020,     // apply the appropriate overlays
OVERLAYINDEX = 0x000000040     // Get the index of the overlay
}
  
能否详细指点下?
上网查得两种结构:typedef struct  { 
         short           level;          /* fill/empty level of buffer */ 
         unsigned        flags;          /* File status flags    */ 
         char            fd;             /* File descriptor      */ 
         unsigned char   hold;           /* Ungetc char if no buffer */ 
         short           bsize;          /* Buffer size          */ 
         unsigned char   *buffer;        /* Data transfer buffer */ 
         unsigned char   *curp;          /* Current active pointer */ 
         unsigned        istemp;         /* Temporary file indicator */ 
         short           token;          /* Used for validity checking */ 
}       FILE;                           /* This is the FILE object */ 

struct _iobuf {
char *_ptr;
int   _cnt;
char *_base;
int   _flag;
int   _file;
int   _charbuf;
int   _bufsiz;
char *_tmpfname;
};
typedef struct _iobuf FILE;不知道哪个是正确的
http://topic.csdn.net/u/20090527/23/ac18a5c3-9668-4972-b694-b575cc554a85.html
  
LZ执着精神值得钦佩啊。
我机器上现在没有C#的开发环境,所以没办法跟你拿出完整代码了,帮你顶顶吧。顺便问一句,大家说了这么多方法,到底你现在的问题是什么?能具体描述一下嘛?
  
http://blog.csdn.net/null1/archive/2009/03/03/3953155.aspx
  
你可以换个思路啊。
其实用C++中的类,说到底还是使用它的共有方法。在C#中,你可以把C++中的使用到的所有方法封装成一个类,从而直接在C#下使用,怎么把C++中的方法在C#中调用我上面已经说过了。
哎,不知道怎么帮你,再帮你顶顶吧。
  
//-------------头文件
#pragma onceclass testclass
{
public:
testclass(void);
~testclass(void);
public:
WCHAR* GetName(void);
};
//-------------CPP
#include "StdAfx.h"
#include "testclass.h"testclass::testclass(void)
{
}testclass::~testclass(void)
{
}
WCHAR* testclass::GetName(void)
{
return NULL;
}//--------------接口头文件
#pragma once#include "testclass.h"class Insertface
{
public:
Insertface(void);
~Insertface(void);
public:
void* CreateTextClass(void);
WCHAR* GetName(void *textClass);
BOOL DeleteTextClass(void *textClass);
};
//----------------------CPP
#include "StdAfx.h"
#include "Insertface.h"Insertface::Insertface(void)
{
}Insertface::~Insertface(void)
{
}
extern "C" _declspec(dllexport) void* Insertface::CreateTextClass(void)
{
testclass *p = new testclass();
return (void *)p;
}
extern "C" _declspec(dllexport) WCHAR* Insertface::GetName(void *textClass)
{
return ((testclass *)textClass)->GetName();
}
extern "C" _declspec(dllexport) BOOL Insertface::DeleteTextClass(void *textClass)
{
delete (testclass *)textClass;
return TRUE;
}//----------------------C#
//C#代码我就不写了,在C#使用long类型来接受void *。指针就被C#记录了,在C++使用dllexport开放类的方法,这样C#就可以使用C++里的类。目前我是这样用的~不知道是不是您要的。。还有如果是C++定义的结构,C#定义一样相同的就可以互相使用了。
  
我最近也遇到了这个问题。
查了一些资料,据说是要用managed C++把native C++ DLL中的类重新封装一下,保留公共方法,再提供给C#调用。
感觉对于复杂的自定义类,其过程过于繁琐,最后决定继续用native C++开发了。
  
不知楼主研究出来没有,我也遇到这个问题.
从网上查到的资料说是加载kernal32这个dll,然后用委托的方式加载C++的LoadLibrary,GetProcAddress,FreeLibrary这几个函数.目前正在研究,如果楼主有什么心得希望一起讨论下henry827@163.com
  
我也遇到了,只是我用P/Invoke而已!
  
看来只有通过GET,SET方法封装成员变量了
  
你需要用managed c++写一个wrapper类,然后c#调用这个wrapper类
  
http://hi.baidu.com/linzi1128/blog/item/dda5371fa7fa40cea6866946.html 这个好像是 不过 我还没成功  先Up了
  
http://hi.baidu.com/linzi1128/blog/item/dda5371fa7fa40cea6866946.html
  
不通过托管C++封装的方法当然有了
你以为微软是吃白饭的呀
上网搜搜Reflection类的用法就知道了
不过呢,执行效率可能只有用C++重新封装的1/3甚至更慢;
呵呵~~
若果实在没办法就给我发E-mail:vc1110@163.com
我可以给你提供一些C#调用C++写的非托管dll里的类的代码(包含继承,重载)我的上网条件不怎么好,一般可能周六才有时间 呵呵~~
  
看看这个
http://topic.csdn.net/u/20090430/17/5148f3de-9f7d-45b1-b5ba-2fcaf06baafc.html
比较有意思
呵呵~~
  
http://topic.csdn.net/u/20090430/17/5148f3de-9f7d-45b1-b5ba-2fcaf06baafc.html
http://www.microsoft.com/china/msdn/library/langtool/vcsharp/csharp05162002.mspx 
  http://blog.csdn.net/dragonsuc/archive/2003/06/30/20791.aspx C# code 
try   
  {   
  //動態加載dll文件,   
  string   sDllName   =   ServiceId.Substring(0,ServiceId.IndexOf("."));   
  Assembly   DllAssembly   =   Assembly.Load(sDllName);   
  //執行選定的類別   
  ISDServer.ITransaction   Tx   =   (ISDServer.ITransaction)DllAssembly.CreateInstance(sDllName+".Transaction");   
  return   Tx.Execute((ISDServer.IProcess)this,ServiceId,oType,sParam); 
如下也可以:  
C# code 
  string   DllPath   =   Application.StartupPath   +   @"\someDLL.dll";   
  System.Reflection.Assembly   assmble   =   System.Reflection.Assembly.LoadFile(DllPath);   
    
  Type   tmpType   =   assmble.GetType("someDLL.ClassA");   
  System.Reflection.MethodInfo   tmpM   =   tmpType.GetMethod("Add");   
  object   tmpobj   =   assmble.CreateInstance("someDLL.ClassA");   
  int   i   =   (int)tmpM.Invoke(tmpobj,   new   object[]{2,3});   
    
  ----------ClassA的定义--------------------   
  using   System;   
    
  namespace   someDLL   
  {   
  ///   <summary>   
  ///   ClassA   的摘要说明。   
  ///   </summary>   
  public   class   ClassA   
  {   
  public   ClassA()   
  {   
  //   
  //   TODO:   在此处添加构造函数逻辑   
  //   
  }   
    
  public   int   Add(int   a,   int   b)   
  {   
  return   a+b;   
  }   
  }   
  }  
  
我也遇到同样的问题,DLL中的C++类,是商家提供的芯片驱动的,在应用程序中,要把该类对象化,才能使用。如何在C#应用程序中,调用该类,并对象化呢。
  
其实这个问题 你要先搞清楚  系统dll(导出函数dll,就是c++直接编译的dll)  和COM的区别
因为c++ 直接编译出来的dll是导出函数的dll(如API系统的dll),其他语言要用的时候一般都是要声明调用的函数的
如:c# 用dllimport后  在extend 声明对应函数的名次(就像用API函数一样);
    vb就是用dlclare声明API的因此这种dll是不建议强行的去调用c++的dll的类,因为编译器的原因,导致生成不一样号码的导出函数(类中的函数也是导出函数模式生成在dll中),这种函数dll在c++内部调用的时候,一般都是还要连接lib文件(导出函数的标号库)因此,要想很好的调用c++的dll中类的话,或者说是调用其他语言所写的dll的话,还是把dll做成COM组件模型进行调用吧,COM就是做这个事情的,
如vb编译的dll就是COM模型的,所以vb的dll调用之前都需要先注册,所以c#调用vb写的dll是很简单的,直接引用就可以了
因此  把c++的dll做成COM模型的就很好了
  
http://blog.csdn.net/starlee/article/details/2864588看看这个,我刚找到的
  
http://hi.baidu.com/daven172/item/94ca33d7649056c71b72b463
要不看看这个先,是调用类的,ATL
  
微软MSDN论坛上的VC++ FAQ里面有个How do I Invoke Native C++ DLL from .NET Code
地址:http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/5df04db1-bbc8-4389-b752-802bc84148fe里面一共介绍了4种方法,代码可以到微软的All-In-One Code里面下载,Browser的下载链接已经在上面的网页中给出了。耐心地看看吧,抱着真心想学习的态度,虽然看下来很痛苦,都是英文的。

c#调用C++写的dll导出类,如何实现相关推荐

  1. dll的概念、dll导出类(转)

    1. DLL的概念 DLL(Dynamic Linkable Library),动态链接库,可以向程序提供一些函数.变量或类.这些可以直接拿来使用. 静态链接库与动态链接库的区别: (1)静态链接库与 ...

  2. QT调用C#写的Dll

    QT调用C#写的Dll 参见: https://blog.csdn.net/weixin_42420155/article/details/81060945 C#写的dll是没有dllMain入口函数 ...

  3. C# 调用C++写的dll的实现方法

    文章目录 **C# 调用C++写的dll的实现方法** 一. dll的编写 2.C#调用 C# 调用C++写的dll的实现方法 C#调用C++的非托管类的dll其实很简单基本就是固定的调用格式. 一. ...

  4. QT调用C++写的Dll

    C#写的dll是没有dllMain入口函数的,是一种中间语言,需要.Net运行时进行做本地化工作,因此如果要调用C#写的dll,需要依赖.Net运行时,然而Qt中还无法直接调用.Net运行时,最好的方 ...

  5. VC调用C#写的DLL

    From: http://hi.baidu.com/ocnc/blog/item/2c7fc45c1d1f1051fbf2c0ea.html VC中调用C#写的DLL时,对C#这边的DLL有要求,即要 ...

  6. 易语言如何调用c dll文件,易语言调用C++写的DLL

    直接调用会弹出堆栈错误的信息,原因是VS默认是__cdcel方式,而易语言是__stdcall,所以调用约定不一致导致堆栈错误. 解决方案很简单,易语言声明DLL函数时"在库中对应命令名&q ...

  7. c++调用c#写的DLL

    c++调用c#写的DLL: 此文章演示了建立c#的dll: c++建立工程,引入dll: 不能解决的问题: 指定dll的路径,在代码里面直接写 #using "xxx.dll" 必 ...

  8. C++dll导出类的方式__declspec(dllexport)

    前言 有时导出dll的时候既想用到整个类又想保持多态 一.直接导出整个类 示例: 头文件 #ifdef AITWapper_EXP //根据项目是导入导出预定义 #define AITWapper_D ...

  9. QT 调用vs写的dll 使用OutputDebugString输出调试信息

    vs c++写的dll中使用的cout 将调试信息写到控制台中,但qt调用dll时,cout内容没有办法显示了. 解决办法:使用OutputDebugStringA char buf[128];spr ...

最新文章

  1. 5大典型模型测试单机训练速度超对标框架,飞桨如何做到?
  2. 移动端网站优化该注意哪些重点?
  3. DRCNN超分辨重建2016年
  4. 这焊接技术,大开眼界了......
  5. Git,Git Flow,GitLab使用指南
  6. 解决“Maven项目中的Dynamic Web Module 3.0 requires Java 1.6 or newer”问题
  7. GCC详解-Binutils工具之readelf
  8. 本机Windows远程操作虚拟机Windows界面
  9. 杰·亚伯拉罕的产品营销35种策略完整版
  10. Altium Designer 18安装方法
  11. 【转】 【技巧 】 数学难题大揭秘:减少计算错误的技术
  12. 玩转基因组浏览器之查看gwas结果
  13. 期望/概率dp 学习报告
  14. kubectl的安装和配置
  15. YYText实现文本与下划线,删除线偏移
  16. ppi 各代iphone_从iPhone 6到iPhone X:居然隐藏了这么多秘密
  17. python effective 骚操作
  18. pytorch——冻结某层参数
  19. Linux系统管理笔记
  20. Gorm之gorm.io/gorm源码

热门文章

  1. 浙江工业大学计算机学院团学,浙江工业大学计算机学院团委学生会人事管理制度.docx...
  2. matlab 贝塞尔曲线,基于MATLAB动态实现Bezier曲线几何作图.pdf
  3. Bezier曲线曲面绘制
  4. Java实现文件写入——IO流(输入输出流详解)
  5. 世界狼疮日:年轻女性多发,患者主要器官受累,却被告知“看起来没有生病” | 美通社头条...
  6. video播放器禁用or启用拖动进度条
  7. 什么是SSH与SSH客户端
  8. 在线教育真的有必要 在线省钱
  9. 企业想要搭建CRM平台该如何操作?
  10. webp转换成jpg,试试看这几种简单的转换方法