AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

need help for dllcall winapi FormatMessage

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Wed Sep 09, 2009 1:45 pm    Post subject: need help for dllcall winapi FormatMessage Reply with quote

the definition of this function is
Code:

DWORD WINAPI FormatMessage(
  __in      DWORD dwFlags,
  __in_opt  LPCVOID lpSource,
  __in      DWORD dwMessageId,
  __in      DWORD dwLanguageId,
  __out     LPTSTR lpBuffer,
  __in      DWORD nSize,
  __in_opt  va_list *Arguments
);


My funtion
Code:

FormatMessageFromSystem(ErrorCode)
{
   VarSetCapacity(Buffer, 500)
   DllCall("FormatMessage"
      , "UInt", 0x1000      ; FORMAT_MESSAGE_FROM_SYSTEM
      , "Int", 0
      , "Int", ErrorCode
      , "Int", 0
      , "UInt", &Buffer
      , "UInt", 500
      , "Str", NULL)
   Return Buffer
}


but it always return null. How should I fix it?
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Wed Sep 09, 2009 6:50 pm    Post subject: Reply with quote

Check out this, can be always useful Wink WindowsDataTypes
This should work
Code:
MsgBox % FormatMessageFromSystem(0x1100)
FormatMessageFromSystem(ErrorCode)
{
   VarSetCapacity(Buffer, 2000)
   DllCall("FormatMessage"
      , "UInt", 0x1000      ; FORMAT_MESSAGE_FROM_SYSTEM
      , "UInt", 0
      , "UInt", ErrorCode
      , "UInt", 0x800 ;LANG_SYSTEM_DEFAULT (LANG_USER_DEFAULT=0x400)
      , "UInt", &Buffer
      , "UInt", 500
      , "UInt", 0)
   VarSetCapacity(buffer,-1)
   Return Buffer
}

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Thu Sep 10, 2009 1:36 pm    Post subject: Reply with quote

Thanks very much.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Fri Sep 11, 2009 5:42 am    Post subject: Reply with quote

VarSetCapacity(buffer,-1) can be avoided:

EITHER

Code:
MsgBox % FormatMessageFromSystem(0x1100)
FormatMessageFromSystem(ErrorCode)
{
   VarSetCapacity(Buffer, 2000)
   DllCall("FormatMessage"
      , "UInt", 0x1000      ; FORMAT_MESSAGE_FROM_SYSTEM
      , "UInt", 0
      , "UInt", ErrorCode
      , "UInt", 0x800 ;LANG_SYSTEM_DEFAULT (LANG_USER_DEFAULT=0x400)
      , "Str", Buffer
      , "UInt", 500
      , "UInt", 0)
   Return Buffer
}


OR

Code:
MsgBox % FormatMessageFromSystem(0x1100)
FormatMessageFromSystem(ErrorCode)
{
   VarSetCapacity(Buffer, 2000, 32 )
   DllCall("FormatMessage"
      , "UInt", 0x1000      ; FORMAT_MESSAGE_FROM_SYSTEM
      , "UInt", 0
      , "UInt", ErrorCode
      , "UInt", 0x800 ;LANG_SYSTEM_DEFAULT (LANG_USER_DEFAULT=0x400)
      , "UInt", &Buffer
      , "UInt", 500
      , "UInt", 0)
   Return Buffer
}
Back to top
View user's profile Send private message Send e-mail
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Fri Sep 11, 2009 6:51 am    Post subject: Reply with quote

Exclamation Thanks SKAN
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group