最近對一些第三方類庫進行c++托管以便c#調用 因為之前沒弄過,出現各種各樣的問題
fatal error LNK1104: 無法打開文件“xxx.lib”或者xxx.dll 等等等
總結:
1.字符集:設置一樣
2.平台:設置一樣,比如32位 就都設置32位,這里千萬要注意:設置平台的時候要一個個看清楚才行 不然在上面設置了解決方案,下面項目有些沒反應(新建的)
3.引用進來的dll還有頭文件,如果頭文件中引用了其他的文件,記得通通給它導進來了
4.這樣還不一定行,還會出現各種未識別,未能解析提示,看你的要引用的頭文件是不是需要引用其他的.lib文件 ,都引用進來試試
5.好了,沒報錯 但是你創建的c#引用c++dll報錯了,未能加載dll,那你要看看托管的工程dll和托管要包裝的dll有沒有復制到c#項目運行目錄下了
6.如果還是不行 c#的項目平台是不是都是一樣的
7.還是不行,用管理員運行你的vs 或者可以先用管理員運行exe看行不行。
類型轉換
c++
// ClassLibrary1.h #pragma once #pragma comment (lib,"libthirdparty.lib") #pragma comment (lib,"libmupdf.lib") #pragma comment (lib,"MuPDFLib-x64.lib") #include"..\MuPDFlib\MuPDFClass.h" using namespace System; namespace MuPDFCv { public ref class MuPDFLG { // TODO: 在此處添加此類的方法。 public: MuPDFLG(); int TLoadPdf(String ^ filename, String ^ password); int TLoadPage(int pageNumber); int TGetCount(); unsigned char * TGetBitmap(int width, int height, float dpiX, float dpiY, int rotation, int colorspace, bool rotateLandscapePages, bool convertToLetter, int & pnLength, int maxSize); int Test(int width); private: MuPDFClass * m_pMuPDFClass; }; }
// 這是主 DLL 文件。 #include "stdafx.h" #include "MuPDFCv.h" using namespace System::Runtime::InteropServices; //#include <msclr\marshal.h> //using namespace msclr::interop; #include <vcclr.h> MuPDFCv::MuPDFLG::MuPDFLG() { m_pMuPDFClass = new MuPDFClass(); } int MuPDFCv::MuPDFLG::Test(int width) { width++; return 2; } unsigned char * MuPDFCv::MuPDFLG::TGetBitmap(int width, int height, float dpiX, float dpiY, int rotation, int colorspace, bool rotateLandscapePages, bool convertToLetter, int & pnLength, int maxSize) { unsigned char *pData = m_pMuPDFClass->GetBitmap(width, height, dpiX, dpiY, rotation, colorspace, rotateLandscapePages, convertToLetter, pnLength, maxSize); return pData; } int MuPDFCv::MuPDFLG::TLoadPdf(String ^ filename, String ^ password) { // 將string轉換成C++能識別的指針 /*char* strFilename = marshal_as<char*>(filename); char* strPassword = marshal_as<char*>(password);*/ char* strFilename = (char*)(void*)Marshal::StringToHGlobalAnsi(filename); char* strPassword = (char*)(void*)Marshal::StringToHGlobalAnsi(password); //Ptr return m_pMuPDFClass->LoadPdf(strFilename, strPassword); } int MuPDFCv::MuPDFLG::TLoadPage(int pageNumber) { return m_pMuPDFClass->LoadPage(pageNumber); } int MuPDFCv::MuPDFLG::TGetCount() { return m_pMuPDFClass->_PageCount; }
c# 怎么引用的:先直接引用dll
代碼:
using MuPDFCv; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DateTime _StartDateTime; private void button1_Click(object sender, EventArgs e) { _StartDateTime = DateTime.Now; int j; //"d:\\1.pdf" string path = @"D:\test\pdf\45x20 number without hologram.pdf"; MuPDFLG mup = new MuPDFLG(); mup.TLoadPdf(path, null); int count = mup.TGetCount(); mup.TLoadPage(1); int h = 0; //CPdf.GetHeight();//得到pdf默認高度 int w = 0; //CPdf.GetWidth(); //CPdf.SetAlphaBits(4); //int iLength = 0;//總字節數w*h*(ch+alph) float iDipX = 600;//如果指定了w,不起作用 float iDipY = 600;//如果指定了h,不起作用 int rot = 0;//-90,90,180 int color = 1;//0:rgba 1:ga byte[] date; unsafe { int iLength = 0; byte* intP = mup.TGetBitmap(w, h, iDipX, iDipY, rot, color, false, false, &iLength, 1000000); date = new byte[iLength]; //用下面的賦值多200多毫秒 //for (int i = 0; i < iLength; i++) //{ // date[i] = *intP; // intP++; //} //用這個用時較少 Marshal.Copy((IntPtr)intP, date, 0, iLength); //Marshal.Copy(intP, date, 0, iLength); } //mup.TGetBitmap(ref w, ref h, iDipX, iDipY, rot, color, false, false, ref iLength, 1000000); //間隔時間 TimeSpan subTract = DateTime.Now.Subtract(_StartDateTime); double _ZhTime = subTract.TotalMilliseconds; label1.Text = _ZhTime.ToString(); } } }
unsafe用這個可以在里面寫非托管代碼 這里要在項目里設置一下