dcsimg
Results 1 to 7 of 7

Thread: This should be easy but there's clearly something I'm missing...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2019
    Posts
    16

    This should be easy but there's clearly something I'm missing...

    I'm building a class to instantiate a JScript/Edge JS runtime which should be compatible with both VBA and VB6 etc.

    My project can be found here.

    The only current issue I'm getting an error on

    Code:
        Private Declare Function IECreateRuntime Lib "jscript9.dll" Alias "JsCreateRuntime" (jsRuntimeAttributes As Integer, jsRuntimeVersion As Long, JsThreadServiceCallback As LongPtr, jsRuntimeHandle As Any) As Long
        Private Declare Function EdgeCreateRuntime Lib "chakra.dll" Alias "JsCreateRuntime" (jsRuntimeAttributes As Integer, JsThreadServiceCallback As LongPtr, jsRuntimeHandle As LongPtr) As Long
        '...
        retl = IECreateRuntime(0, -1, 0, VarPtr(pRuntime)) 'KEEP GETTING ERRORS?
        '... and ...
        retl = EdgeCreateRuntime(0, 0, VarPtr(pRuntime))  'KEEP GETTING ERRORS?
    The error I am getting seems to be an argument error, and thus I suspect I've misunderstood something about how to declare the functions...

    The documentation for calling these functions can be found here:

    https://docs.microsoft.com/en-us/mic...ntime-function

    Any ideas?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,277

    Re: This should be easy but there's clearly something I'm missing...

    It would be helpful to tell us exactly what the error says. Also, are your running this in VBA or VB6? VB6 does not support vartypes of LongPtr and Integer probably should be declared as Long in your API declarations. Why? JsCreateRuntime in jscript9.dll disassembled asm is popping 16 bytes from the stack, so it is expecting 4 Longs. Likewise, the function in chakra.dll is popping 12 bytes, expecting 3 Longs.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2019
    Posts
    16

    Re: This should be easy but there's clearly something I'm missing...

    Quote Originally Posted by LaVolpe View Post
    It would be helpful to tell us exactly what the error says.
    I should clarify my statement actually. There is no VB runtime error specifically. Rather, neither IECreateRuntime nor EdgeCreateRuntime return S_OK. They both return 65537 aka 0x10001 which somehow I figured was an argument error?

    Quote Originally Posted by LaVolpe View Post
    Also, are your running this in VBA or VB6? VB6 does not support vartypes of LongPtr and Integer probably should be declared as Long in your API declarations.
    Oh! I actually didn't realise there was a difference... Thanks for that information, I never knew... In response to your question I am currently running it in VBA, but I guess it'd be a good idea to use Longs directly instead so it is compatible with both?

    Quote Originally Posted by LaVolpe View Post
    Why? JsCreateRuntime in jscript9.dll disassembled asm is popping 16 bytes from the stack, so it is expecting 4 Longs. Likewise, the function in chakra.dll is popping 12 bytes, expecting 3 Longs.
    Indeed! Odd stuff I know. That's what the documentation claims:

    Code:
    // Microsoft Edge mode signature  
    STDAPI_(JsErrorCode) JsCreateRuntime(  
       _In_ JsRuntimeAttributes attributes,  
       _In_opt_ JsThreadServiceCallback threadService,  
       _Out_ JsRuntimeHandle *runtime);  
      
    // Legacy mode signature  
    STDAPI_(JsErrorCode) JsCreateRuntime(  
       _In_ JsRuntimeAttributes attributes,  
       _In_ JsRuntimeVersion version,  
       _In_opt_ JsThreadServiceCallback threadService,  
       _Out_ JsRuntimeHandle *runtime  
    );

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2019
    Posts
    16

    Re: This should be easy but there's clearly something I'm missing...

    Quote Originally Posted by sancarn View Post
    I should clarify my statement actually. There is no VB runtime error specifically. Rather, neither IECreateRuntime nor EdgeCreateRuntime return S_OK. They both return 65537 aka 0x10001 which somehow I figured was an argument error?
    Ah it was from this post on stack overflow:

    https://stackoverflow.com/a/56412578/6302131

    Code:
    const (
        JsNoError = 0
        JsErrorInvalidArgument = 0x10001
        JsErrorNullArgument = 0x10002
    
        JsErrorScriptCompile = 0x30002
    )
    Again, not super sure whether it is an invalid argument error or not though...

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,277

    Re: This should be easy but there's clearly something I'm missing...

    This probably should've been posted in the Office portion of the forums.

    Regarding LongPtr and VBA, don't just simply change that to Long. It matters in VBA, depending on whether the O/S is 64 or 32 bit. But if running on 64 bit, I'd think you should've gotten an error for not defining the API declarations using the keyword PtrSafe.

    Regarding invalid arguments. Try this. Your last parameters in the APIs is ByRef. One declaration has the last parameter defined As LongPtr while the other is As Any. Probably want to be consistent there. Instead of passing VarPtr(pRuntime), just pass pRuntime. ByRef usage will pass the variable by pointer. Otherwise you you may want to change those last parameters to ByVal & As LongPtr (or Long), then pass param as: VarPtr(pRuntime). You should verify that the other parameter values you are passing are acceptable, within the enumerations provided by those dlls.

    I'll bow out of this thread. I have no experience with those specific APIs and if you get past this problem of yours, you'll likely have other questions regarding the APIs.
    Last edited by LaVolpe; Jul 21st, 2019 at 10:12 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2019
    Posts
    16

    Re: This should be easy but there's clearly something I'm missing...

    Quote Originally Posted by LaVolpe View Post
    This probably should've been posted in the Office portion of the forums.
    Regarding LongPtr and VBA, don't just simply change that to Long. It matters in VBA, depending on whether the O/S is 64 or 32 bit. But if running on 64 bit, I'd think you should've gotten an error for not defining the API declarations using the keyword PtrSafe.
    Indeed it's 4 bytes on 32 bit systems and 8 bytes on 64 bit systems correct? I'd be fairly surprised this couldn't be overcome by simply using conditional #if statements..

    Quote Originally Posted by LaVolpe View Post
    Regarding invalid arguments. Try this. Your last parameters in the APIs is ByRef. One declaration has the last parameter defined As LongPtr while the other is As Any. Probably want to be consistent there.
    Yeah I was arbitrarily trying different things to see if I could get it to work. I'm still not confident I know what VB does under the hood to any arguments, and/or what's the difference between ByRef X as Y and ByVal X as Y = VarPtr(X) if that makes sense.... Generally I struggle to convert C/C++ declarations into VB declarations... Unsure where I can learn that better, apart from just continuous practice...

    Quote Originally Posted by LaVolpe View Post
    Instead of passing VarPtr(pRuntime), just pass pRuntime. ByRef usage will pass the variable by pointer. Otherwise you you may want to change those last parameters to ByVal & As LongPtr (or Long), then pass param as: VarPtr(pRuntime). You should verify that the other parameter values you are passing are acceptable, within the enumerations provided by those dlls.
    I'll give these a go thanks EDIT: Yeah doesn't look like either of those fix the issue. And the other parameters are at least correct (or at least the AHK sample https://github.com/Lexikos/ActiveScr...r/JsRT.ahk#L21 I'm porting uses the same parameters...)

    Quote Originally Posted by LaVolpe View Post
    I'll bow out of this thread. I have no experience with those specific APIs and if you get past this problem of yours, you'll likely have other questions regarding the APIs.
    Thanks for the help, it's at least a start. I'm mostly porting this from AHK, so I generally know roughly how it's meant to work. The main thing I struggle with is trying to understand what VB does to what I write... Thanks for the help though!
    Last edited by sancarn; Jul 21st, 2019 at 11:25 AM.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2019
    Posts
    16

    Re: This should be easy but there's clearly something I'm missing...

    Oh my god. I solved it...

    Code:
    Private Declare Function IECreateRuntime Lib "jscript9.dll" Alias "JsCreateRuntime" ( _ 
      jsRuntimeAttributes As Integer, _ 
      jsRuntimeVersion As Long, _ 
      JsThreadServiceCallback As LongPtr, _ 
      ByVal jsRuntimeHandle As LongPtr _ 
    ) As Long
    -->

    Code:
    Private Declare Function IECreateRuntime Lib "jscript9.dll" Alias "JsCreateRuntime" ( _ 
      ByVal jsRuntimeAttributes As Integer, _ 
      ByVal jsRuntimeVersion As Long, _ 
      ByVal JsThreadServiceCallback As LongPtr, _ 
      ByVal jsRuntimeHandle As LongPtr _ 
    ) As Long
    Genuinely it would be really useful if there was some kind of C --> VB call convention converter... lol

    Thanks LaVolpe for teaching me that ByRef params are automatically passed by pointer. That made everything make a lot more sense.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width