-
Jul 20th, 2019, 06:24 PM
#1
Thread Starter
Member
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?
-
Jul 20th, 2019, 06:41 PM
#2
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.
-
Jul 21st, 2019, 07:27 AM
#3
Thread Starter
Member
Re: This should be easy but there's clearly something I'm missing...
Originally Posted by LaVolpe
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?
Originally Posted by LaVolpe
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?
Originally Posted by LaVolpe
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
);
-
Jul 21st, 2019, 08:54 AM
#4
Thread Starter
Member
Re: This should be easy but there's clearly something I'm missing...
Originally Posted by sancarn
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...
-
Jul 21st, 2019, 10:04 AM
#5
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.
-
Jul 21st, 2019, 11:16 AM
#6
Thread Starter
Member
Re: This should be easy but there's clearly something I'm missing...
Originally Posted by LaVolpe
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..
Originally Posted by LaVolpe
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...
Originally Posted by LaVolpe
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...)
Originally Posted by LaVolpe
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.
-
Jul 21st, 2019, 11:36 AM
#7
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|