FolderItem with REALLoadObjectMethod crashing plugin

I’m trying to use REALLoadObjectMethod to call FolderItem inside of a plugin. I have been referencing the PluginSDK examples to create this. My code is the same as the examples apart from the change to a FolderItem, but this is crashing when calling the plugin in Xojo.

//FilePath is REALString containing file directory (c:/User/Program Files...)
static REALclassRef sFolderItem = REALGetClassRef("FolderItem");
typedef void (*FolderItemFuncTy)(REALobject, REALstring path);

REALobject FolderItemObj = REALnewInstanceWithClass(sFolderItem); //Have also tried REALnewInstance
FolderItemFuncTy FolderItem = (FolderItemFuncTy)REALLoadObjectMethod(FolderItemObj, "FolderItem( path as String )");
FolderItem(FolderItemObj, FilePath); //Plugin crashes here

Very hard to diagnose as it’s a DLL. Everything seems right but I can’t seem to figure out why this is crashing my project?

You put in nil checks for sFolderItem and FolderItem pointers?

I would think the method should be “Constructor” and not “FolderItem”

I have not checked in Xojo if there is valid constructor with only string or not. Note though if there are optional parameters then in plugin you have to have those also.

And of course as Christian hints to check for Nil.

I have changed my code to now include nil checks, parsing all the optional parameters for the constructor, and changing the REALLoadObjectMethod call from Constructor to FolderItem.

The folder item constructor requires FolerItem.PathModes (FolderItem.PathModes - Xojo Documentation) passed as an enum which i have setup like this.

static const char *testEnumFields[] = {
	"Native = 0",
	"Shell = 1",
	"URL = 2",

static REALenum FolderItemEnums[] = {
	{"PathModes", NULL, REALScopeGlobal, testEnumFields, 3}

And then passed into the FolderItem object as:

FolderItem(FolderItemObj, FilePath, FolderItemEnums[1], true);

But i am now getting “The PathMode is not recognized” as a InvalidArgumentException bug inside Xojo IDE.

Am i constructing/using the REALEnum incorrectly? It seems to be correct as per the examples.

For pathMode, please declare as RBInteger and pass the number, e.g. 0.

The problem with trying to pass 0 into the constructor is that it must be used like this,

static REALobject CreateXojoFolderItem(REALstring FilePath, RBInteger pathMode, bool followAlias)

	FolderItemFuncTy FolderItem = (FolderItemFuncTy)REALLoadObjectMethod(FolderItemObj, "Constructor( path as String, pathMode as FolderItem.PathModes, followAlias as Boolean ) As FolderItem");
	FolderItem(FolderItemObj, FilePath, pathMode = 1, followAlias = true);

I was hoping that it would be possible to choose the PathMode enum with an integer but it does not work as when inside of Xojo IDE the plugin is called like this,

Dim f as FolderItem
//CreateXojoFolderItem requires 3 values
 f = XojoPluginName.CreateXojoFolderItem("C:\User\ProgramFiles\....", 1, true)

And is erroring on the integer value as it’s asking for “FolderItem.PathMode…”. If the PathMode is passed it will crash as the plugin is setting it to an integer.

My problem is that i’m unable to pass the object “FolderItem.PathMode…” inside of the FolderItem method call in c++, but trying to do this in Xojo IDE also doesn’t work?

Do you already check for nil?

FolderItemFuncTy FolderItem = (FolderItemFuncTy)REALLoadObjectMethod(FolderItemObj, “Constructor( path as String, pathMode as FolderItem.PathModes, followAlias as Boolean ) As FolderItem”);

Because constructor has no return value!
So this function pointer is nil as it’s not found.
And when calling it, the result is undefined.

For any people in the future reading this post,

The issue was that i was trying to create the FolderItem Object as a REALObject, where it’s really supposed to be a REALfolderItem which i found while looking through the header files.

Changed my method type to be REALfolderItem along with anything that was a REALobject. changed the constructor statement to:

"Constructor( path as String, pathMode as FolderItem.PathModes, followAlias as Boolean )"

Another stitch up was that i was assuming that the FolderItem.PathModes enums were 0 or 1 based as going off the documentation, therefore trying 0 or 1 to get Native path… Turns out it’s not. It’s only shown in Xojo that it’s not as shown, i should have been using 3 to get Native:

So now using FolderItem with the correct object type and parsing 3 to give a native path, it’s all working :slight_smile:

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.