• c#引用c++dll和c++导出类出现的各种问题


    最近对一些第三方类库进行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"..MuPDFlibMuPDFClass.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 <msclrmarshal.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:	estpdf45x20 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用这个可以在里面写非托管代码  这里要在项目里设置一下

    
    
  • 相关阅读:
    [转]用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
    xp_cmdshell开启与关闭
    [转]触发器
    上午绿茶下午菊花茶晚上枸杞
    SQL中CONVERT转化日期函数的使用方法
    如何使用url实现数据交互
    struts2之form标签theme属性详解
    [保险核心] 保险公司集中收付费系统
    数据库分页大全
    如何使用spring中hibernate返回获取list集合
  • 原文地址:https://www.cnblogs.com/lsgsanxiao/p/11239254.html
Copyright © 2020-2023  润新知