The Combined Programming of VS2008 and Matlab

Today, let’s do one thing: take a combined programming with VS2008 and matlab2009a.

 

Configuration of Matlab

First, launch matlab and do the following to configure the initial environment :(the red part is my input)

mbuild-setup

Please choose yourcompiler for building standalone MATLAB applications:

Would you like mbuild to locate installed compilers [y]/n?y

Select a compiler:

[1] Lcc-win32 C2.4.1 in C:\PROGRA~1\MATLAB\R2009a\sys\lcc

[0] None

Compiler: 0

  mbuild: No compiler selected. No actiontaken.

 

mbuild -setup

Please choose your compiler forbuilding standalone MATLAB applications:

Would you like mbuild to locateinstalled compilers [y]/n?n

Select a compiler:

[1] Lcc-win32 C 2.4.1

[2] Microsoft Visual C++ 6.0

[3] Microsoft Visual C++ .NET 2003

[4] Microsoft Visual C++ 2005 SP1

[5] Microsoft Visual C++ 2008Express

[6] Microsoft Visual C++ 2008 SP1

[0] None

 

Compiler: 6(make sure your vs2008 is the official version, not the express)

 

The default location for Microsoft Visual C++ 2008 compilers is C:\Program Files\Microsoft Visual Studio9.0,

but that directory does not existon this machine. 

Use C:\Program Files\MicrosoftVisual Studio 9.0 anyway [y]/n? y

Please verify your choices:

Compiler: Microsoft Visual C++ 2008

Location: C:\ProgramFiles\Microsoft Visual Studio 9.0

 

Are these correct [y]/n?y

 

****************************************************************************

 Warning: Applications/components generated using Microsoft VisualC++     

           2008 require that the MicrosoftVisual Studio 2008 run-time      

           libraries be available on thecomputer used for deployment.      

           To redistribute yourapplications/components, be sure that the   

           deployment machine has theserun-time libraries.                 

****************************************************************************

 

Trying to update options file:C:\Documents and Settings\Administrator\ApplicationData\MathWorks\MATLAB\R2009a\compopts.bat

From template:             C:\PROGRA~1\MATLAB\R2009a\bin\win32\mbuildopts\msvc90freecompp.bat

 

Done . . .

 

second, create a m file, naming it “add.m”. Here are in add.m:

 

function y = add(a,b)

y = a + b;

end

 

third, type this in matlab command prompt:

mcc –W cpplib:myadd–T link:lib add.m –C(concrete parameters in HELP document)

One or two minutes later, there are several new files in your current directory, just like:

 

Please notice: the four files myadd.ctf, myadd.h, myadd.lib, myadd.dll should be used by vs2008 project.So you must copy them to your current vs project directory.

 

You may feel strange to file myadd.ctf. However, if you do not copy it, you’ll get this in executing stage if other operations are OK:

 


By the way, By typing the following command at VS2008 command prompt, we can get the exports and imports of myadd.dll.Please notice the red part, because we're gonna use it in our own code.

>> dumpbin myadd.dll /exports > exports.txt

here is the exports.txt:

Microsoft (R) COFF/PE Dumper Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.
Dump of file myadd.dll

File Type: DLL

  Section contains the following exports for myadd.dll

    00000000 characteristics
    50F6AA97 time date stamp Wed Jan 16 21:26:47 2013
        0.00 version
           1 ordinal base
           7 number of functions
           7 number of names

    ordinal hint RVA      name

          1    0 000018D0 ?add@@YAXHAAVmwArray@@ABV1@1@Z
          2    1 00001560 mlxAdd
          3    2 000014E0 myaddGetMcrID
          4    3 000014A0
myaddInitialize
          5    4 00001440 myaddInitializeWithHandlers
          6    5 000014F0 myaddPrintStackTrace
          7    6 000014C0 myaddTerminate
  Summary
        2000 .data
        4000 .rdata
        2000 .reloc
        9000 .text

>> dumpbin myadd.dll /imports > imports.txt

here is the imports.txt:

Microsoft (R) COFF/PE Dumper Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file myadd.dll
File Type: DLL
  Section contains the following imports:
   
KERNEL32.dll
              1000A000 Import Address Table
              1000C8D8 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference


                  1F4 GetModuleFileNameA
                  1F6 GetModuleHandleA
                  2A6 HeapSize
                  2E3 LCMapStringW
                  2E1 LCMapStringA
                  240 GetStringTypeW
                  31A MultiByteToWideChar
                  23D GetStringTypeA
                  1E8 GetLocaleInfoA
                  2B5 InitializeCriticalSectionAndSpinCount
                  …… many functions!

    mclmcrrt710.dll
              1000A0F4 Import Address Table
              1000C9CC Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference


                  12C mclcppGetArrayBuffer_proxy
                    A array_buffer_set_proxy
                    6 array_buffer_add_proxy
                  12A mclcppFeval_proxy
                    8 array_buffer_get_proxy
                    C array_buffer_to_cell_proxy
                   A9 mclFeval_proxy
                   DF mclGetStackTrace_proxy
                   AC mclFreeStackTrace_proxy
                   BC mclGetID_proxy
                  11F mclTerminateInstance_proxy
                  131 mclmcrInitialize_proxy
                   EC mclInitializeComponentInstance_proxy
                  126 mclWrite_proxy
                   4C array_ref_getV_int_proxy
                   66 array_ref_number_of_elements_proxy
                   44 array_ref_classID_proxy
                  12D mclcppGetLastError_proxy
                   81 error_info_get_message_proxy
                  1C3 ref_count_obj_release_proxy
                  1C2 ref_count_obj_addref_proxy
                  128 mclcppCreateError_proxy
  Summary
        2000 .data
        4000 .rdata
        2000 .reloc
        9000 .text


Configuration of VS2008

 

create a MFC project based on dialog named myadd, then do the following to contain “include and lib”:

 choose tool-option and add the matlab’s include directory. see below:

 

choose tool-option and add the matlab’s lib directory.see below:

 

Then you’re expected to enter menu project-myadd property-linker-input, do these:

 

so myadd.lib was generated just now, and the mclmcrrt.lib is necessary for myadd.dll.

The configuration of vs is completed. let’s enter the code process.

 

Implementation of basic function

 

open your myadd project and add some controller such as the static text, the edit control and button. Just like this:

 

double click the calculate button and automatically enter the message-handling function void CMyAddTestDlg::OnBnClickedOk().

 

edit it like this:

void CMyAddTestDlg::OnBnClickedOk()
{
  // TODO: 在此添加控件通知处理程序代码
  CString str;
  inta,b,c;
 
//get what’s in the edit control and save to a,b
  a=GetDlgItemInt(IDC_EDIT1);
  b=GetDlgItemInt(IDC_EDIT2);
 
//initializing function to avoid failure
  if(!myaddInitialize())
  {
     str.Format(_T("lib myadd2 Initialize failed!\n"));
     MessageBox(str);
     return;
  }
  try
  {
//declare a mwArray variable and store a to it
     mwArray mwa(1,1,mxUINT32_CLASS);
     mwa.SetData(&a,1);
 
//declare a mwArray variable and store b to it
     mwArray mwb(1,1,mxUINT32_CLASS);
     mwb.SetData(&b,1);
 
//declare a mwArray variable and store the result
     mwArray mwy(1,1,mxUINT32_CLASS);
 
//call the add function and store mwy to c
     add(1,mwy,mwa,mwb);
     mwy.GetData(&c,1);
 
     SetDlgItemInt(IDC_EDIT3,c);
  }
  catch(mwException&e)
  {
     str.Format(_T("%s"),e.what());
     MessageBox(str);
  }
 
//the ending function
  myaddTerminate();
 
  //OnOK();youshould add an annotation of it
}

And do not forget add the include file “myadd.h” to MyAddDlg.cpp !

 

So you are seeming to be allowed to link and run the program. If you do that, you may become a little disappointed. Because you’re likely to get the following error:

 

the way to it is simple.open targetver.h and modify two lines:

 

that’s to say,change the two “0600” to “0501”.

 

Then you try to run the application. To your disappointment again, you’ll get this:

 

For this problem,you may refer to this:

http://www.mathworks.ch/matlabcentral/newsreader/view_thread/249680

 

Here is my way:

find the file \MATLAB\R2009a\toolbox\compiler\deploy\win32\MCRInstaller.exe and install it. when it’s done, the installing directory will have that .dll file.In order to find it in executing stage, you should set your PATH environment variable.See below:

 

And this time,believe me ,you will get a satisfying answer if your quality is not so bad…Just see below


Congratulations !!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值