Skip to main content
GameDev.net gamedev.net
🔒 Locked

win32 api in c#

Started by supagu Sep 12, 2005 at 6:05 AM 1 replies 800+ views
Original Post
supagu
supagu
saw this article: http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/ Now, im trying to call a win32 api commands but they are failing. I have a c++ dll that opens a window and i can get the handle of the window. I use this handle in c# to try to get the window text but it fails. I'm using swig, which generates wrapper code for my c++ dll which i use in my c# project. so i have a function prototype for example as (genrated by swig): SWIGTYPE_p_HWND GetWindowHandle(); where SWIGTYPE_p_HWND is defined as:

public class SWIGTYPE_p_HWND {
  private HandleRef swigCPtr;

  internal SWIGTYPE_p_HWND(IntPtr cPtr, bool futureUse) {
    swigCPtr = new HandleRef(this, cPtr);
  }

  protected SWIGTYPE_p_HWND() {
    swigCPtr = new HandleRef(null, IntPtr.Zero);
  }

  internal static HandleRef getCPtr(SWIGTYPE_p_HWND obj) {
    return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
  }
}
so, i call GetWindowText like so:

if (GetWindowText(SWIGTYPE_p_HWND.getCPtr(hwnd).Handle.ToInt32(), Buff, 256) > 0)
                    {
                        int nothing = 0;
                    }
but it fails. I've checked to make sure i can get a valid hwnd from my dll using a c++ test that uses the code, and it works fine. any ideas? Cheers, Fabian
MrEvil
MrEvil
(Article, Linkified).

Define "fails", please [grin].



How have you declared GetWindowTextand Buff?
GetWindowText should be declared with the [DllImportAttribute("user32.dll")] attribute, and Buff should be declared as a StringBuilder.
supagu
supagu
as in it does not get inside the if statement

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.