Hit-Parade .VB Research Center . Compteur
Accueil ~  Code ~  Programmes ~  Api ~  Forum ~  Cours ~  Livres ~  Quiz ~  Annuaire
VB Research Center > Code >
appel d\'un DLL Matlab
[S'enregistrer] [Profil] [Préférences] [Se déconnecter] [Rechercher]
[Répondre]
Auteur Message
thi
Junior

appel d'un DLL Matlab

Bonjour,

j'essaie d'appeler une DLL créée par la compilation d'un programme d'un logiciel de calcul (Matlab):
en fait l'aide technique m'a donné une méthode (ci-dessous pour ceux qui ont le courage) pour appeler la DLL depuis Visual C++ (et ça marche).

mais pour un appel depuis Visual Basic, je me suis inspiré d'une autre aide Matlab, et ça ne marche pas!

Quelqu'un peut-il m'aider? merci
===============================================
Voici les informations pour comprendre:

-les fichiers suivants après les étapes Visual C++

Dans \interfaceVB-Matlab\: foo.m, foo.c, foo.h,
foolib.dll,foolib.c,foolib.h, foolib.exports, foolib.lib, foolib.mlib, foolib.exp

\interfaceVB-Matlab\test\: foowrap.c,
test.dsp, test.dsw,test.ncb,test.opt, test.plg

\interfaceVB-Matlab\test\Debug\: foowrap.obj,
test.exe, test.ilk, test.pch, test.pdb,
vc60.idb, vc60.pdb
(puis j'ai copié le .dll ici pour pouvoir faire fonctionner le .exe sous Visual C++).

-la déclaration suivante dans l'application VB:

Private Declare Function foowrap Lib "C:\Stage\logiciels\interfaceVB-Matlab\foolib.dll" (ByVal x As Double) As Double

-l'appel:

Private Sub Command1_Click()
y = foowrap(1.1)
MsgBox y
End Sub

entraine lors de l'exécution l'erreur:

point d'entrée foowrap d'une DLL introuvable dans "C:\Stage\logiciels\interfaceVB-Matlab\foolib.dll"

===============================================
Aide Matlab:

For example:

In MATLAB,

1. Write a M-file function named foo and saved as foo.m that contains
the following code:

%%%BEGIN CODE %%%

function y = foo(x)
y = x+1;

%%%END CODE %%%

2. Use the MATLAB Compiler to compile foo.m into C code that can be
included in a C shared library:

%%%BEGIN CODE %%%

mcc -t -L C -W lib:foolib -T link:lib foo.m libmmfile.mlib

%%%END CODE%%%

This will generate the following files:

foo.c
foo.h
foolib.c
foolib.h
foolib.exports
foolib.dll
foolib.exp
foolib.mlib

In Microsoft Visual C/C++ 6.0

1. Start up the Microsoft Visual C/C++ 6.0 IDE

2. Go to FILE and NEW. Click on Projects Tab. Select Win32 Console
Application. In the Project Name field type "test". "Create new
workspace" should be filled in. In the "Platforms" field, "Win32 should
also be checked. Click OK.

3. Select the File View tab and double click on test files. Select
Source Files, select Project menu option, select Add to Project and then
click on New. In the Files tab select C++ Source file, check Add to
project and type "foowrap.c" in the File name field.

4. Enter the following code in the foowrap.c file:

%%%BEGIN CODE %%%

#include "foolib.h"
#include "matlab.h"
#include <stdio.h>

void main(void)
{
mxArray *x_ptr;
mxArray *y_ptr;
double *y;
double ret;

/* Create an mxArray to input into mlfFoo */
x_ptr = mlfScalar(1);

/* Call the library initialization function */
foolibInitialize();

/* Call the implementation function */
y_ptr = mlfFoo(x_ptr);

/* Call the library termination function */
foolibTerminate();

/* The return value from mlfFoo is an mxArray so we must extract the
data from it */
y = mxGetPr(y_ptr);
ret = *y;

/* Print a double precision number*/
printf("The output of foo is %f\n",ret);
}

%%%END CODE %%%

5. Right click on test files and select Add Files to Project. Select
foolib.lib and the following files located in
$MATLAB\extern\lib\win32\microsoft\msvc60:

libmatlb.lib
libmmfile.lib
libmx.lib

6. Highlight test files and right click. Select Settings. Click on the
C/C++ Tab. In the Category listbox select Code Generation. In the Use
Runtime library listbox select Multithreaded DLL. Change the Category
listbox to Preprocessor. Add to the Preprocessor definitions MSVC,
MSWIND, IBMPC so:

WIN32,_DEBUG,_CONSOLE,_MBCS

changes to:

WIN32,_DEBUG,_CONSOLE,_MBCS,MSVC,MSWIND,IBMPC

Add to the Additional include directories field:

$MATLAB\extern\include\cpp;$MATLAB\extern\include

where $MATLAB is the MATLAB installation directory such as d:\matlabR12.

Also add to the Additional include directories field the directory in
which foolib.h is located.
Click OK

7. Go to Build and Rebuild All.

8. You should now have built test.exe.

9. You may also need to place foolib.dll in the same location as
test.exe.

23 Apr 2002 @ 08:39 Voir le profil de thi   Envoyer un message à thi Modifier ce message
Doc VB
Admin

Re:appel d\'un DLL Matlab

Ah! Les appels de fonctions DLL... un sujet pour le moins épineux.
Les raisons du message d'erreur peuvent être multiples. Si tu as un peu de courage (et de temps), il me semble qu'un point de base de connaissance Microsoft explique comment créer une DLL pour l'appeler depuis VB.
Il faut que tu vérifies la déclaration C de la fonction (__declspec(dllexport) ou quelque chose dans le genre).
Il me semble qu'il faut déclarer la fonction dans un fichier ".DEF".
Enfin, il faut peut-être ajouter le mot clé "Alias" dans ta délcaration :
Private Declare Function foowrap Lib "C:\Stage\logiciels\interfaceVB-Matlab\foolib.dll" Alias #1 (ByVal x As Double) As Double
En générale les fonctions sont numérotées et il faut utiliser ce numéro pour la déclaration afin de faire la liaison.

----------------------
-~[ Doc VB ~ webmaster ]~-
Visual Basic Research Center

23 Apr 2002 @ 23:59 Voir le profil de Doc VB   Envoyer un message à Doc VB Modifier ce message
Heure GMT
[Répondre]
Administration: Ouvrir / Fermer / Supprimer / Déplacer
Accès rapide :
Visual Basic Research Center - (c) 2000/2002 -  Webmaster : docvb (chez) free (point) fr