标题:VC2005 在C++下编写的函数如何生成.dll文件
只看楼主
高小
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2010-6-30
结帖率:82.35%
已结贴  问题点数:10 回复次数:5 
VC2005 在C++下编写的函数如何生成.dll文件
我现在是在C++结合OpenCV编写的小波变换和逆变换函数,请问该如何生成.dll文件以供C#调用啊?代码如下:
#include "stdafx.h"
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

// 二维离散小波变换(单通道浮点图像)
void DWT(IplImage *pImage, int nLayer)
{
 // 执行条件
 if (pImage)
 {
  if (pImage->nChannels == 1 &&
   pImage->depth == IPL_DEPTH_32F &&
   ((pImage->width >> nLayer) << nLayer) == pImage->width &&
   ((pImage->height >> nLayer) << nLayer) == pImage->height)
  {
   int     i, x, y, n;
   float   fValue   = 0;
   float   fRadius  = sqrt(2.0f);
   int     nWidth   = pImage->width;
   int     nHeight  = pImage->height;
   int     nHalfW   = nWidth / 2;
   int     nHalfH   = nHeight / 2;
   float **pData    = new float*[pImage->height];
   float  *pRow     = new float[pImage->width];
   float  *pColumn  = new float[pImage->height];
   for (i = 0; i < pImage->height; i++)
   {
    pData[i] = (float*) (pImage->imageData + pImage->widthStep * i);
   }
   // 多层小波变换
   for (n = 0; n < nLayer; n++, nWidth /= 2, nHeight /= 2, nHalfW /= 2, nHalfH /= 2)
   {
    // 水平变换
    for (y = 0; y < nHeight; y++)
    {
     // 奇偶分离
     memcpy(pRow, pData[y], sizeof(float) * nWidth);
     for (i = 0; i < nHalfW; i++)
     {
      x = i * 2;
      pData[y][i] = pRow[x];
      pData[y][nHalfW + i] = pRow[x + 1];
     }
     // 提升小波变换
     for (i = 0; i < nHalfW - 1; i++)
     {
      fValue = (pData[y][i] + pData[y][i + 1]) / 2;
      pData[y][nHalfW + i] -= fValue;
     }
     fValue = (pData[y][nHalfW - 1] + pData[y][nHalfW - 2]) / 2;
     pData[y][nWidth - 1] -= fValue;
     fValue = (pData[y][nHalfW] + pData[y][nHalfW + 1]) / 4;
     pData[y][0] += fValue;
     for (i = 1; i < nHalfW; i++)
     {
      fValue = (pData[y][nHalfW + i] + pData[y][nHalfW + i - 1]) / 4;
      pData[y][i] += fValue;
     }
     // 频带系数
     for (i = 0; i < nHalfW; i++)
     {
      pData[y][i] *= fRadius;
      pData[y][nHalfW + i] /= fRadius;
     }
    }
    // 垂直变换
    for (x = 0; x < nWidth; x++)
    {
     // 奇偶分离
     for (i = 0; i < nHalfH; i++)
     {
      y = i * 2;
      pColumn[i] = pData[y][x];
      pColumn[nHalfH + i] = pData[y + 1][x];
     }
     for (i = 0; i < nHeight; i++)
     {
      pData[i][x] = pColumn[i];
     }
     // 提升小波变换
     for (i = 0; i < nHalfH - 1; i++)
     {
      fValue = (pData[i][x] + pData[i + 1][x]) / 2;
      pData[nHalfH + i][x] -= fValue;
     }
     fValue = (pData[nHalfH - 1][x] + pData[nHalfH - 2][x]) / 2;
     pData[nHeight - 1][x] -= fValue;
     fValue = (pData[nHalfH][x] + pData[nHalfH + 1][x]) / 4;
     pData[0][x] += fValue;
     for (i = 1; i < nHalfH; i++)
     {
      fValue = (pData[nHalfH + i][x] + pData[nHalfH + i - 1][x]) / 4;
      pData[i][x] += fValue;
     }
     // 频带系数
     for (i = 0; i < nHalfH; i++)
     {
      pData[i][x] *= fRadius;
      pData[nHalfH + i][x] /= fRadius;
     }
    }
   }
   delete[] pData;
   delete[] pRow;
   delete[] pColumn;
  }
 }
}
// 二维离散小波恢复(单通道浮点图像)
void IDWT(IplImage *pImage, int nLayer)
{
 // 执行条件
 if (pImage)
 {
  if (pImage->nChannels == 1 &&
   pImage->depth == IPL_DEPTH_32F &&
   ((pImage->width >> nLayer) << nLayer) == pImage->width &&
   ((pImage->height >> nLayer) << nLayer) == pImage->height)
  {
   int     i, x, y, n;
   float   fValue   = 0;
   float   fRadius  = sqrt(2.0f);
   int     nWidth   = pImage->width >> (nLayer - 1);
   int     nHeight  = pImage->height >> (nLayer - 1);
   int     nHalfW   = nWidth / 2;
   int     nHalfH   = nHeight / 2;
   float **pData    = new float*[pImage->height];
   float  *pRow     = new float[pImage->width];
   float  *pColumn  = new float[pImage->height];
   for (i = 0; i < pImage->height; i++)
   {
    pData[i] = (float*) (pImage->imageData + pImage->widthStep * i);
   }
   // 多层小波恢复
   for (n = 0; n < nLayer; n++, nWidth *= 2, nHeight *= 2, nHalfW *= 2, nHalfH *= 2)
   {
    // 垂直恢复
    for (x = 0; x < nWidth; x++)
    {
     // 频带系数
     for (i = 0; i < nHalfH; i++)
     {
      pData[i][x] /= fRadius;
      pData[nHalfH + i][x] *= fRadius;
     }
     // 提升小波恢复
     fValue = (pData[nHalfH][x] + pData[nHalfH + 1][x]) / 4;
     pData[0][x] -= fValue;
     for (i = 1; i < nHalfH; i++)
     {
      fValue = (pData[nHalfH + i][x] + pData[nHalfH + i - 1][x]) / 4;
      pData[i][x] -= fValue;
     }
     for (i = 0; i < nHalfH - 1; i++)
     {
      fValue = (pData[i][x] + pData[i + 1][x]) / 2;
      pData[nHalfH + i][x] += fValue;
     }
     fValue = (pData[nHalfH - 1][x] + pData[nHalfH - 2][x]) / 2;
     pData[nHeight - 1][x] += fValue;
     // 奇偶合并
     for (i = 0; i < nHalfH; i++)
     {
      y = i * 2;
      pColumn[y] = pData[i][x];
      pColumn[y + 1] = pData[nHalfH + i][x];
     }
     for (i = 0; i < nHeight; i++)
     {
      pData[i][x] = pColumn[i];
     }
    }
    // 水平恢复
    for (y = 0; y < nHeight; y++)
    {
     // 频带系数
     for (i = 0; i < nHalfW; i++)
     {
      pData[y][i] /= fRadius;
      pData[y][nHalfW + i] *= fRadius;
     }
     // 提升小波恢复
     fValue = (pData[y][nHalfW] + pData[y][nHalfW + 1]) / 4;
     pData[y][0] -= fValue;
     for (i = 1; i < nHalfW; i++)
     {
      fValue = (pData[y][nHalfW + i] + pData[y][nHalfW + i - 1]) / 4;
      pData[y][i] -= fValue;
     }
     for (i = 0; i < nHalfW - 1; i++)
     {
      fValue = (pData[y][i] + pData[y][i + 1]) / 2;
      pData[y][nHalfW + i] += fValue;
     }
     fValue = (pData[y][nHalfW - 1] + pData[y][nHalfW - 2]) / 2;
     pData[y][nWidth - 1] += fValue;
     // 奇偶合并
     for (i = 0; i < nHalfW; i++)
     {
      x = i * 2;
      pRow[x] = pData[y][i];
      pRow[x + 1] = pData[y][nHalfW + i];
     }
     memcpy(pData[y], pRow, sizeof(float) * nWidth);
    }
   }
   delete[] pData;
   delete[] pRow;
   delete[] pColumn;
  }
 }
}
搜索更多相关主题的帖子: 通道 
2010-12-30 18:54
ml232528
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:367
专家分:879
注 册:2007-7-23
得分:10 
一 项目属性里找到配置属性、常规、项目默认值、配置类型,修改(应用程序.exe)为(动态库.dll)就可以了。

二 声明要导出的函数        
1 头文件中
    extern "c" void _declspec(dllexport)DWT(IplImage *pImage, int nLayer);
2 def文件中
    EXPORTS
        DWT @1 ;注释 @1为序号可以不要

-︻┻┳═一 ☆ 悲伤的代价就是让自己明白什么是最重要的和应该珍惜的
2010-12-30 21:51
高小
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2010-6-30
得分:0 
回复 2楼 ml232528
这个是在什么地方写啊 是在win32项目->Dll文件中写,然后生成Dll文件吗?麻烦说仔细一些!我没弄过这个!
2010-12-31 09:25
高小
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2010-6-30
得分:0 
回复 2楼 ml232528
你看我这是在win32项目里的Dll文件中写的但生成不了,C#也不知道怎么调用?请求帮忙!
// mydll.h头文件中代码
#ifdef mydll_api
#else
#define mydll_api _declspec(dllimport)
#endif
mydll_api void DWT(IplImage *pImage, int nLayer);
mydll_api void IDWT(IplImage *pImage, int nLayer);

//Wavelet.cpp源文件代码
#define mydll_api _declspec(dllexport)

#include "MyDll.h"
#include "cv.h"
#include "highgui.h"

// 二维离散小波变换(单通道浮点图像)
void DWT(IplImage *pImage, int nLayer)
{
 // 执行条件
 if (pImage)
 {
  if (pImage->nChannels == 1 &&
   pImage->depth == IPL_DEPTH_32F &&
   ((pImage->width >> nLayer) << nLayer) == pImage->width &&
   ((pImage->height >> nLayer) << nLayer) == pImage->height)
  {
   int     i, x, y, n;
   float   fValue   = 0;
   float   fRadius  = sqrt(2.0f);
   int     nWidth   = pImage->width;
   int     nHeight  = pImage->height;
   int     nHalfW   = nWidth / 2;
   int     nHalfH   = nHeight / 2;
   float **pData    = new float*[pImage->height];
   float  *pRow     = new float[pImage->width];
   float  *pColumn  = new float[pImage->height];
   for (i = 0; i < pImage->height; i++)
   {
    pData[i] = (float*) (pImage->imageData + pImage->widthStep * i);
   }
   // 多层小波变换
   for (n = 0; n < nLayer; n++, nWidth /= 2, nHeight /= 2, nHalfW /= 2, nHalfH /= 2)
   {
    // 水平变换
    for (y = 0; y < nHeight; y++)
    {
     // 奇偶分离
     memcpy(pRow, pData[y], sizeof(float) * nWidth);
     for (i = 0; i < nHalfW; i++)
     {
      x = i * 2;
      pData[y][i] = pRow[x];
      pData[y][nHalfW + i] = pRow[x + 1];
     }
     // 提升小波变换
     for (i = 0; i < nHalfW - 1; i++)
     {
      fValue = (pData[y][i] + pData[y][i + 1]) / 2;
      pData[y][nHalfW + i] -= fValue;
     }
     fValue = (pData[y][nHalfW - 1] + pData[y][nHalfW - 2]) / 2;
     pData[y][nWidth - 1] -= fValue;
     fValue = (pData[y][nHalfW] + pData[y][nHalfW + 1]) / 4;
     pData[y][0] += fValue;
     for (i = 1; i < nHalfW; i++)
     {
      fValue = (pData[y][nHalfW + i] + pData[y][nHalfW + i - 1]) / 4;
      pData[y][i] += fValue;
     }
     // 频带系数
     for (i = 0; i < nHalfW; i++)
     {
      pData[y][i] *= fRadius;
      pData[y][nHalfW + i] /= fRadius;
     }
    }
    // 垂直变换
    for (x = 0; x < nWidth; x++)
    {
     // 奇偶分离
     for (i = 0; i < nHalfH; i++)
     {
      y = i * 2;
      pColumn[i] = pData[y][x];
      pColumn[nHalfH + i] = pData[y + 1][x];
     }
     for (i = 0; i < nHeight; i++)
     {
      pData[i][x] = pColumn[i];
     }
     // 提升小波变换
     for (i = 0; i < nHalfH - 1; i++)
     {
      fValue = (pData[i][x] + pData[i + 1][x]) / 2;
      pData[nHalfH + i][x] -= fValue;
     }
     fValue = (pData[nHalfH - 1][x] + pData[nHalfH - 2][x]) / 2;
     pData[nHeight - 1][x] -= fValue;
     fValue = (pData[nHalfH][x] + pData[nHalfH + 1][x]) / 4;
     pData[0][x] += fValue;
     for (i = 1; i < nHalfH; i++)
     {
      fValue = (pData[nHalfH + i][x] + pData[nHalfH + i - 1][x]) / 4;
      pData[i][x] += fValue;
     }
     // 频带系数
     for (i = 0; i < nHalfH; i++)
     {
      pData[i][x] *= fRadius;
      pData[nHalfH + i][x] /= fRadius;
     }
    }
   }
   delete[] pData;
   delete[] pRow;
   delete[] pColumn;
  }
 }
}
// 二维离散小波恢复(单通道浮点图像)
void IDWT(IplImage *pImage, int nLayer)
{
 // 执行条件
 if (pImage)
 {
  if (pImage->nChannels == 1 &&
   pImage->depth == IPL_DEPTH_32F &&
   ((pImage->width >> nLayer) << nLayer) == pImage->width &&
   ((pImage->height >> nLayer) << nLayer) == pImage->height)
  {
   int     i, x, y, n;
   float   fValue   = 0;
   float   fRadius  = sqrt(2.0f);
   int     nWidth   = pImage->width >> (nLayer - 1);
   int     nHeight  = pImage->height >> (nLayer - 1);
   int     nHalfW   = nWidth / 2;
   int     nHalfH   = nHeight / 2;
   float **pData    = new float*[pImage->height];
   float  *pRow     = new float[pImage->width];
   float  *pColumn  = new float[pImage->height];
   for (i = 0; i < pImage->height; i++)
   {
    pData[i] = (float*) (pImage->imageData + pImage->widthStep * i);
   }
   // 多层小波恢复
   for (n = 0; n < nLayer; n++, nWidth *= 2, nHeight *= 2, nHalfW *= 2, nHalfH *= 2)
   {
    // 垂直恢复
    for (x = 0; x < nWidth; x++)
    {
     // 频带系数
     for (i = 0; i < nHalfH; i++)
     {
      pData[i][x] /= fRadius;
      pData[nHalfH + i][x] *= fRadius;
     }
     // 提升小波恢复
     fValue = (pData[nHalfH][x] + pData[nHalfH + 1][x]) / 4;
     pData[0][x] -= fValue;
     for (i = 1; i < nHalfH; i++)
     {
      fValue = (pData[nHalfH + i][x] + pData[nHalfH + i - 1][x]) / 4;
      pData[i][x] -= fValue;
     }
     for (i = 0; i < nHalfH - 1; i++)
     {
      fValue = (pData[i][x] + pData[i + 1][x]) / 2;
      pData[nHalfH + i][x] += fValue;
     }
     fValue = (pData[nHalfH - 1][x] + pData[nHalfH - 2][x]) / 2;
     pData[nHeight - 1][x] += fValue;
     // 奇偶合并
     for (i = 0; i < nHalfH; i++)
     {
      y = i * 2;
      pColumn[y] = pData[i][x];
      pColumn[y + 1] = pData[nHalfH + i][x];
     }
     for (i = 0; i < nHeight; i++)
     {
      pData[i][x] = pColumn[i];
     }
    }
    // 水平恢复
    for (y = 0; y < nHeight; y++)
    {
     // 频带系数
     for (i = 0; i < nHalfW; i++)
     {
      pData[y][i] /= fRadius;
      pData[y][nHalfW + i] *= fRadius;
     }
     // 提升小波恢复
     fValue = (pData[y][nHalfW] + pData[y][nHalfW + 1]) / 4;
     pData[y][0] -= fValue;
     for (i = 1; i < nHalfW; i++)
     {
      fValue = (pData[y][nHalfW + i] + pData[y][nHalfW + i - 1]) / 4;
      pData[y][i] -= fValue;
     }
     for (i = 0; i < nHalfW - 1; i++)
     {
      fValue = (pData[y][i] + pData[y][i + 1]) / 2;
      pData[y][nHalfW + i] += fValue;
     }
     fValue = (pData[y][nHalfW - 1] + pData[y][nHalfW - 2]) / 2;
     pData[y][nWidth - 1] += fValue;
     // 奇偶合并
     for (i = 0; i < nHalfW; i++)
     {
      x = i * 2;
      pRow[x] = pData[y][i];
      pRow[x + 1] = pData[y][nHalfW + i];
     }
     memcpy(pData[y], pRow, sizeof(float) * nWidth);
    }
   }
   delete[] pData;
   delete[] pRow;
   delete[] pColumn;
  }
 }
}

提示错误:错误    1    error C2065: 'IplImage' : undeclared identifier    e:\my documents\vs 2005\visual studio 2005\my documents\vs c++.net\c_dwt-idwt_dll\c_dwt-idwt_dll\mydll.h    6   
错误    2    error C2065: 'pImage' : undeclared identifier    e:\my documents\vs 2005\visual studio 2005\my documents\vs c++.net\c_dwt-idwt_dll\c_dwt-idwt_dll\mydll.h    6   
错误    3    error C2062: type 'int' unexpected    e:\my documents\vs 2005\visual studio 2005\my documents\vs c++.net\c_dwt-idwt_dll\c_dwt-idwt_dll\mydll.h    6   
错误    4    error C2062: type 'int' unexpected    e:\my documents\vs 2005\visual studio 2005\my documents\vs c++.net\c_dwt-idwt_dll\c_dwt-idwt_dll\mydll.h    7   
错误    5    error C2065: 'IplImage' : undeclared identifier    e:\my documents\vs 2005\visual studio 2005\my documents\vs c++.net\c_dwt-idwt_dll\c_dwt-idwt_dll\mydll.h    6   
错误    6    error C2065: 'pImage' : undeclared identifier    e:\my documents\vs 2005\visual studio 2005\my documents\vs c++.net\c_dwt-idwt_dll\c_dwt-idwt_dll\mydll.h    6   
错误    7    error C2062: type 'int' unexpected    e:\my documents\vs 2005\visual studio 2005\my documents\vs c++.net\c_dwt-idwt_dll\c_dwt-idwt_dll\mydll.h    6   
错误    8    error C2062: type 'int' unexpected    e:\my documents\vs 2005\visual studio 2005\my documents\vs c++.net\c_dwt-idwt_dll\c_dwt-idwt_dll\mydll.h    7   
错误    9    error C2378: 'IplImage' : redefinition; symbol cannot be overloaded with a typedef    d:\program files\opencv\cxcore\include\cxtypes.h    393   
错误    10    error C2143: syntax error : missing ';' before '*'    d:\program files\opencv\cxcore\include\cxcore.h    97   
错误    11    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int    d:\program files\opencv\cxcore\include\cxcore.h    97   
错误    12    error C2371: 'IplImage' : redefinition; different basic types    d:\program files\opencv\cxcore\include\cxcore.h    97   
错误    13    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int    d:\program files\opencv\cxcore\include\cxcore.h    97   
错误    14    error C2143: syntax error : missing ';' before '*'    d:\program files\opencv\cxcore\include\cxcore.h    100   
错误    15    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int    d:\program files\opencv\cxcore\include\cxcore.h    100   
错误    16    error C2371: 'IplImage' : redefinition; different basic types    d:\program files\opencv\cxcore\include\cxcore.h    100   
错误    17    error C2065: 'image' : undeclared identifier    d:\program files\opencv\cxcore\include\cxcore.h    100   
错误    18    error C2275: 'CvSize' : illegal use of this type as an expression    d:\program files\opencv\cxcore\include\cxcore.h    100   
错误    19    error C2146: syntax error : missing ')' before identifier 'size'    d:\program files\opencv\cxcore\include\cxcore.h    100   
警告    20    warning C4229: anachronism used : modifiers on data are ignored    d:\program files\opencv\cxcore\include\cxcore.h    100   
错误    21    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int    d:\program files\opencv\cxcore\include\cxcore.h    100   
错误    22    error C2078: too many initializers    d:\program files\opencv\cxcore\include\cxcore.h    100   
错误    23    error C2275: 'CvSize' : illegal use of this type as an expression    d:\program files\opencv\cxcore\include\cxcore.h    100   
错误    24    error C2059: syntax error : ')'    d:\program files\opencv\cxcore\include\cxcore.h    102   
错误    25    error C2143: syntax error : missing ';' before '*'    d:\program files\opencv\cxcore\include\cxcore.h    105   
错误    26    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int    d:\program files\opencv\cxcore\include\cxcore.h    105   
错误    27    error C2371: 'IplImage' : redefinition; different basic types    d:\program files\opencv\cxcore\include\cxcore.h    105   
错误    28    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int    d:\program files\opencv\cxcore\include\cxcore.h    105   
警告    29    warning C4229: anachronism used : modifiers on data are ignored    d:\program files\opencv\cxcore\include\cxcore.h    108   
错误    30    error C2182: 'cvReleaseImageHeader' : illegal use of type 'void'    d:\program files\opencv\cxcore\include\cxcore.h    108   
错误    31    fatal error C1903: unable to recover from previous error(s); stopping compilation    d:\program files\opencv\cxcore\include\cxcore.h    108   
2010-12-31 09:35
ml232528
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:367
专家分:879
注 册:2007-7-23
得分:0 
'IplImage' : undeclared identifier
应该是 IplImage 在头文件中引用了 但没有定义
加上有IplImage定义的头文件
 

mydll_api void IDWT(IplImage *pImage, int nLayer);
 void DWT(IplImage *pImage, int nLayer)
{
......
}
 // 函数的声明和定义不一样


-︻┻┳═一 ☆ 悲伤的代价就是让自己明白什么是最重要的和应该珍惜的
2010-12-31 19:57
ml232528
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:367
专家分:879
注 册:2007-7-23
得分:0 
你可以用向导生成一个dll 看看他是怎么导出函数的
文件->新建->项目->win32控制台程序->下一步-> 选中dll 选中导出符号
例子中有 函数 类 变量 的导出

-︻┻┳═一 ☆ 悲伤的代价就是让自己明白什么是最重要的和应该珍惜的
2010-12-31 20:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-330300-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.463615 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved