I have my C# DLL with the functions visible and accessible from PB.
In this example I have 2 functions:
- The first one shows a simple example of addition of two numbers.
- The second one connects to a Wordpress blog and posts an article.
The Sum function works correctly.
The post function in Wordpress does not, but if I compile the library in Console Application mode, it does work correctly and posts.
Does anyone know what could be the reason?
Thanks a lot and I hope @RSbasic reads this and can help me
My PB Code:
Code: Select all
EnableExplicit
Prototype CSharpADDsample(a, b)
Prototype CreatePosttest(URL.p-Unicode, username.p-Unicode, password.p-Unicode)
Define PBEx_WordpressPCL = OpenLibrary(#PB_Any, "PB.Ex_WordpressPCL.dll")
If PBEx_WordpressPCL
Debug "lib ok"
Define CSharpADDsample.CSharpADDsample = GetFunction(PBEx_WordpressPCL, "CSharpADDsample")
Define CreatePosttest.CreatePosttest = GetFunction(PBEx_WordpressPCL, "CreatePosttest")
Else
Debug "lib fail"
End
EndIf
Debug "Csharp add sample: " + CSharpADDsample(3, 5); will return 8
Define blogURL.s = "http://blogdemo.com";
Define myUSER.s = "demouser";
Define myPWD.s = "demopwd";
Debug CreatePosttest(blogURL, myUSER, myPWD)
Delay(5000)
CloseLibrary(PBEx_WordpressPCL)
My C# Lib code (netstandard 2.0):
Code: Select all
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using WordPressPCL;
using WordPressPCL.Models;
namespace WordPressTest
{
class Program
{
static void Main(string[] args)
{
CreatePosttest("http://blogdemo.com", "demouser","demopwd");
}
[RGiesecke.DllExport.DllExport]
public static int CSharpADDsample(int a, int b)
{
return a + b;
}
[RGiesecke.DllExport.DllExport]
public static int CreatePosttest([MarshalAs(UnmanagedType.LPWStr)] string URL, [MarshalAs(UnmanagedType.LPWStr)] string username, [MarshalAs(UnmanagedType.LPWStr)] string password)
{
int res = 0;
try
{
WordPressClient client = new WordPressClient(URL + "/wp-json");
client.AuthMethod = AuthMethod.JWT;
client.RequestJWToken(username, password).Wait();
if (client.IsValidJWToken().GetAwaiter().GetResult())
{
var post = new Post
{
Title = new Title("New post test"),
Content = new Content("Content for new post.")
};
client.Posts.Create(post).Wait();
res = 1;
}
}
catch (Exception e)
{
Console.WriteLine("Error:" + e.Message);
res = 0;
}
return res;
}
}
}
ZIP FILE with BOTH SOURCE CODES: https://drive.google.com/file/d/18Q9tPX ... sp=sharing