⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydll.cpp

📁 《MATLAB实用指南》系列丛书源代码。
💻 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函数,**注:不在MyDll.h中进行函数申明
double OutPut(int n, double *rP)
{
 int i;  double *sum,sum0; 
 FILE *fp1;
 
sum=new double[1];//////////////////内存在管理机制为Cpp机制
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;//////////内存在管理机制为Cpp机制
return sum0; /////// Cpp程序返回计算值
}

//////////////////////////////接口函数,**注:不在MyDll.h中进行函数申明
void mexFunction( 
int nlhs, mxArray *plhs[], 
int nrhs, const mxArray *prhs[] 
)
{int i; 
double *rP,*lP,sum; 
mxArray *lP0;
////////////////// 由 prhs[] 中取得Matlab输入数据
rP=new double[nrhs]; //////////内存在管理机制为Cpp机制
for(i=0;i<nrhs;i++)rP[i]=mxGetScalar(prhs[i]);
/////////////////  调用Cpp程序
sum=OutPut(nrhs,rP);
////////////////   进行数据类型转换,并存入 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); //使用内存数据拷贝函数mxDuplicateArray()
mxDestroyArray(lP0); ///////////内存在管理机制为Matlab机制
delete rP;   
delete lP;
}
////////////////////////结束

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -