?? mydll.cpp
字號:
// MyDll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "MyDll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CMyDllApp
BEGIN_MESSAGE_MAP(CMyDllApp, CWinApp)
//{{AFX_MSG_MAP(CMyDllApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDllApp construction
CMyDllApp::CMyDllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CMyDllApp object
CMyDllApp theApp;
/////////////////////////////////用戶C函數(shù),**注:不在MyDll.h中進(jìn)行函數(shù)申明
double OutPut(int n, double *rP)
{
int i; double *sum,sum0;
FILE *fp1;
sum=new double[1];//////////////////內(nèi)存在管理機(jī)制為Cpp機(jī)制
if((fp1=fopen("OutPut1.txt","w"))==NULL){printf(" Can't open OutPutfile\n");exit(0);}
*sum=0.0;
fprintf(fp1,"Input %d Data is:",n);
for(i=0;i<n;i++){*sum=*sum+rP[i];fprintf(fp1," %7.2lf ",rP[i]);}
fprintf(fp1,"\nsum= %7.2lf \n",*sum);
fclose(fp1);
sum0=*sum;
delete sum;//////////內(nèi)存在管理機(jī)制為Cpp機(jī)制
return sum0; /////// Cpp程序返回計算值
}
//////////////////////////////接口函數(shù),**注:不在MyDll.h中進(jìn)行函數(shù)申明
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]
)
{int i;
double *rP,*lP,sum;
mxArray *lP0;
////////////////// 由 prhs[] 中取得Matlab輸入數(shù)據(jù)
rP=new double[nrhs]; //////////內(nèi)存在管理機(jī)制為Cpp機(jī)制
for(i=0;i<nrhs;i++)rP[i]=mxGetScalar(prhs[i]);
///////////////// 調(diào)用Cpp程序
sum=OutPut(nrhs,rP);
//////////////// 進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換,并存入 plhs[] 返回Matlab
nlhs=nrhs;
plhs[0]=mxCreateDoubleMatrix(1,nlhs,mxREAL);
lP0=mxCreateDoubleMatrix(1,nlhs,mxREAL);
lP= new double[nlhs];
for(i=0;i<nlhs;i++)lP[i]=i*sum;
for(i=0;i<nlhs;i++)*(mxGetPr(lP0)+i)=*(lP+i);
plhs[0]=mxDuplicateArray(lP0); //使用內(nèi)存數(shù)據(jù)拷貝函數(shù)mxDuplicateArray()
mxDestroyArray(lP0); ///////////內(nèi)存在管理機(jī)制為Matlab機(jī)制
delete rP;
delete lP;
}
////////////////////////結(jié)束
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -