1

I've seen few examples that work for me. So I wanted to ask a new question. I am specifically trying to use the AccessibleObjectFromWindow method from oleacc.dll.

I have a version of a function, written by someone else, in Access VBA that I am trying to port to C#.

the code in question is:

    public static bool GetReferenceToApplication(long hWndXL, out object oApplication)
{
    oApplication = null;
    long hWinDesk = (long)FindWindowEx(new IntPtr(hWndXL), new IntPtr(0), "XLDESK", string.Empty);
    long hWin7 = (long)FindWindowEx(new IntPtr(hWinDesk), new IntPtr(0), "EXCEL7", string.Empty);
    Guid guid = new Guid("00020400-0000-0000-C000-000000000046");
    object obj = null;

    if (AccessibleObjectFromWindow(new IntPtr(hWin7), Convert.ToUInt32("&HFFFFFFF0", 16), ref guid, ref obj) == RETURN_OK)
    {
        oApplication = obj;
        return true;
    }
    else return false;
}

Convert.ToUInt32("&HFFFFFFF0", 16) does not work. I know I am doing something wrong here, I do not know what exactly that is. In VBA, I think the ampersand denotes a long. But, I'm not too familiar with this sort of thing yet. Any help would be appreciated. I get an exception now matter which way I try to convert this value to a uint.

JasonG
  • 127
  • 1
  • 8
  • Does this answer your question? [How to convert this hex string into a long?](https://stackoverflow.com/questions/16569951/how-to-convert-this-hex-string-into-a-long) – Trevor Feb 19 '20 at 15:17
  • 2
    Typical vb code, it is all pretty wrong. But use 0xfffffff0 to get ahead. The return type of FindWindowEx() is IntPtr, those longs are the most egregiously wrongness. – Hans Passant Feb 19 '20 at 15:20
  • Thanks, I was planning on converting those to IntPtr after i got everything working. I had copied over the longs from the VBA code, obviously. I appreciate the help! – JasonG Feb 19 '20 at 15:23
  • thank you. I don't ask too many questions here. usually because I find the answer or figure it out on my own. But, in this situation, I am in unfamiliar territory and under a time constraint. I appreciate the advice. Thanks for editing my post, I was about to do it, saved me the effort. lol – JasonG Feb 19 '20 at 15:32
  • 2
    @JasonG Here's the reason, `vb` and `vba` don't natively support unsigned types and hence the reason for `&H` to denote it. In `C#` it does support unsigned integers and starts with `0x` for hex literals, but `&H` is `0x` in `C#`, either replace that and or remove it. – Trevor Feb 19 '20 at 15:37
  • 1
    @Çöđěxěŕ that makes sense! Thanks for your input! Learned something today. :) – JasonG Feb 19 '20 at 15:38
  • @JasonG you're welcome. – Trevor Feb 19 '20 at 15:38

1 Answers1

3

Just remove &H from the string. VBA uses that to indicate "this is a hex value" but it isn't part of a valid hex string that C# can interpret.

Convert.ToUInt32("FFFFFFF0", 16)