FormatMessage

cs_sphinx2 Messages postés 2 Date d'inscription lundi 25 juillet 2005 Statut Membre Dernière intervention 8 juin 2009 - 5 juin 2009 à 14:04
cs_sphinx2 Messages postés 2 Date d'inscription lundi 25 juillet 2005 Statut Membre Dernière intervention 8 juin 2009 - 8 juin 2009 à 07:39
Bonjour,

J'essaie d'appeler FormatMessage sous visual studio C++ 2008 en Windows forms donc en C++ "managé".

Mon code est celui-c mais ne compile pas: voir Error dans les commentaires du code
==========================================================

#include "StdAfx.h"

#include <windows.h>

#include <tchar.h>

#include <strsafe.h>

#include "Constants.h"

#include "manageDateFile.h"

#using <mscorlib.dll>

#using <System.dll>

using namespace System::Diagnostics ;

using namespace System::Text;

using namespace System::Collections;

using namespace System::Runtime::InteropServices;

[DllImport("kernel32.dll", CharSet=System::Runtime::InteropServices::CharSet::Auto)]

int FormatMessage(int dwFlags,

  IntPtr *lpSource,

  int dwMessageId,

  StringBuilder lpBuffer,

  int dwLanguageId,

  int nSize,

  IntPtr *Arguments);

// Constructor

manageDateFile::manageDateFile(bool _fDebug){

fDebug = _fDebug;

}

// Show the mesage for last Error error

void manageDateFile::showLE(String^ lpszFunction, DWORD dw)

{

// Retrieve the system error messa ge for the last-error code

int messageSize  = 255;

    String^ lpMsgBuf = "";

    int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;

    IntPtr ptrlpSource  = IntPtr::Zero;

    IntPtr prtArguments = IntPtr::Zero;

    int retVal = FormatMessage(

dwFlags,

&ptrlpSource,

dw,

0,

&lpMsgBuf,

messageSize,

&prtArguments);  //<-----Error 1 error C2665:
'FormatMessage' : none of the 2 overloads could convert all the
argument types v:\dev\cpp\ChgDate\ChgDate\manageDateFile.cpp 48 ChgDate

/*

    // Display the error message

// Manage

String^ dispMsg;

StringCchPrintf((LPTSTR)lpDisplayBuf,

        LocalSize(lpDisplayBuf) / sizeof(TCHAR),

        TEXT("%s failed with error %d: %s"),

        lpszFunction, dw, lpMsgBuf);

    Debug::WriteLine((LPCTSTR)lpDisplayBuf);

    LocalFree(lpMsgBuf);

*/

}

==========================================================

Avez vous un exemple qui fonctionne?

Merci par avance.

2 réponses

cs_rt15 Messages postés 3874 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 7 novembre 2014 13
6 juin 2009 à 14:26
Salut,

Peut être le StringBuilder/String^ ?

Aide sur le pinvoke de FormatMessage. Ils proposent une alternative d'ailleurs.
0
cs_sphinx2 Messages postés 2 Date d'inscription lundi 25 juillet 2005 Statut Membre Dernière intervention 8 juin 2009
8 juin 2009 à 07:39
Bonjour,
J'ai finalement trouvé une solution
La voici:
#include "StdAfx.h"
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
#include "Constants.h"
#include "manageDateFile.h"
#using <mscorlib.dll>
#using <System.dll>
using namespace System::Diagnostics ;
using namespace System::Text;
using namespace System::Collections;
using namespace System;
using namespace System::Runtime::InteropServices;

// Fonctionne
[DllImport("kernel32.dll", CharSet=System::Runtime::InteropServices::CharSet::Auto)]
int LocalFree(System::IntPtr
);
                        [DllImport("user32.dll", CharSet=System::Runtime::InteropServices::CharSet::Auto)]
int MessageBox(IntPtr, StringBuilder^ text, String^ caption, unsigned int type);

[DllImport("kernel32.dll", CharSet=System::Runtime::InteropServices::CharSet::Auto)]
int FormatMessage(
                    int        dwFlags,
                    System::IntPtr* lpSource,
                    int        dwMessageId,
                    int        dwLanguageId,
                    System::IntPtr* lpMsgBuf,
                    int        nSize,
                    System::IntPtr);

// Constructor
manageDateFile::manageDateFile(bool _fDebug){
    fDebug = _fDebug;
}
// Show the mesage for last Error error
void manageDateFile::showLE(String^ myMessage, DWORD dw)
{
    // Retrieve the system error message for the last-error code
    IntPtr lpMsgBuf      = IntPtr::Zero;
    IntPtr ptrlpSource   = IntPtr::Zero;
    StringBuilder^ text  = gcnew StringBuilder;
  
    int retVal = FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        &ptrlpSource,
        dw,                // Message ID
        MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),                    // LANG
        &lpMsgBuf,
        255,
        IntPtr::Zero); // &prtArguments);
    String^ textMsg =System::Runtime::InteropServices::Marshal::PtrToStringAuto(lpMsgBuf);    text->AppendFormat("sys message {0}, Code {1}, in {2]", textMsg, dw, myMessage);
        MessageBox(IntPtr::Zero, text, "SYSTEM ERROR",  1);
        // Free the pointer
    LocalFree(lpMsgBuf);
}
0
Rejoignez-nous