Fighting with C# and WordpressPCL library from PB [SOLVED]

Just starting out? Need help? Post your questions and find answers here.
zikitrake
Addict
Addict
Posts: 834
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Fighting with C# and WordpressPCL library from PB [SOLVED]

Post by zikitrake »

Inspired by the work @RSBasic did with his PBEx_WebGadget libraries, I'm trying to do something similar to post in Wordpress.

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.

Image

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
Last edited by zikitrake on Thu May 06, 2021 10:21 pm, edited 1 time in total.
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Fighting with C# and WordpressPCL library from PB

Post by RSBasic »

I can't test with WordPress library.
You can return the e.Message as string to PB, then you know what error occurred.
Example:

Code: Select all

[RGiesecke.DllExport.DllExport]
		public static int CreatePosttest([MarshalAs(UnmanagedType.LPWStr)] string URL, [MarshalAs(UnmanagedType.LPWStr)] string username, [MarshalAs(UnmanagedType.LPWStr)] string password, System.IntPtr ErrorOutput)
		{
			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);

				if (ErrorOutput != IntPtr.Zero)
				{
					string ErrorMessage = e.Message.ToString();
					if (ErrorMessage.Length > 120)
					{
						ErrorMessage = ErrorMessage.Substring(0, 120);
					}
					byte[] ErroroutputBytes = System.Text.Encoding.Unicode.GetBytes(ErrorMessage + "\0");
					System.Runtime.InteropServices.Marshal.Copy(ErroroutputBytes, 0, ErrorOutput, ErroroutputBytes.Length);
				}

				res = 0;
			}
			return res;
		}

Code: Select all

EnableExplicit
  
Prototype CSharpADDsample(a, b)
Prototype CreatePosttest(URL.p-Unicode, username.p-Unicode, password.p-Unicode, ErrorOutput)

Define PBEx_WordpressPCL = OpenLibrary(#PB_Any, "PB.Ex_WordpressPCL.dll") 

Global ErrorOutput$ = Space(128)

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, , @ErrorOutput$)
Debug "Error message: " + ErrorOutput$
Delay(5000)
CloseLibrary(PBEx_WordpressPCL)

(Not tested)
Image
Image
zikitrake
Addict
Addict
Posts: 834
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Re: Fighting with C# and WordpressPCL library from PB

Post by zikitrake »

RSBasic wrote: Thu May 06, 2021 4:50 pm...
You can return the e.Message as string to PB, then you know what error occurred...
Dear, RSBasic. First of all, I am very glad to read you again.

Thank you for reply. Now, with your code I got this error: error sending request

So I remembered that with another program to read wordpress articles I had a similar error and I "solved" it by changing the security protocol, adding this line at the begining of the CreatePosttest() function :

Code: Select all

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Now everything works correctly!

Again, thanks a lot, @RSBasic for enlightening me :)
Post Reply