Pelles C forum

C language => Windows questions => Topic started by: bitcoin on November 24, 2023, 06:21:04 PM

Title: How to convert this sh*tcode to C?
Post by: bitcoin on November 24, 2023, 06:21:04 PM
Can anybody tell me, what this code means and how to convert it to pure C? Is this possible?

Code: [Select]
// <-- WebView2 sample code starts here -->
// Step 3 - Create a single WebView within the parent window
// Locate the browser and set up the environment for WebView
CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr,
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[hWnd](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {

// Create a CoreWebView2Controller and get the associated CoreWebView2 whose parent is the main window hWnd
env->CreateCoreWebView2Controller(hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
[hWnd](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
if (controller != nullptr) {
webviewController = controller;
webviewController->get_CoreWebView2(&webviewWindow);
}
Title: Re: How to convert this sh*tcode to C?
Post by: TimoVJL on November 24, 2023, 08:52:11 PM
Not easily, something wrong with that code.
Reformat it better way.
Title: Re: How to convert this sh*tcode to C?
Post by: bitcoin on November 24, 2023, 09:27:21 PM
Hello, Timo!
Can you see whole code here ? https://github.com/arsher/WebView2GettingStarted
I don't understand , what means Callback<..> , -> and other
Title: Re: How to convert this sh*tcode to C?
Post by: TimoVJL on November 25, 2023, 10:00:55 AM
These give some hints:

https://stackoverflow.com/questions/66820213/basic-win32-webview2-example-not-working-in-pure-c

https://github.com/MicrosoftEdge/WebView2Feedback/issues/1124

https://stackoverflow.com/questions/70768030/usage-icorewebview2environmentoptions-in-cbuilder-11

and

https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/environment-controller-core?tabs=win32cpp

https://www.nuget.org/packages/Microsoft.Web.WebView2

Title: Re: How to convert this sh*tcode to C?
Post by: bitcoin on November 25, 2023, 12:47:03 PM
Thank you TimoVJL!
Why don't they (micro$oft) want , that we to use the C language? First in GDI+, where you can't call them with C. Yes, a visual studio (ms sdk) does not allow use flat Api! You can try it. Only PellesC includes work!
Title: Re: How to convert this sh*tcode to C?
Post by: bitcoin on November 25, 2023, 01:12:52 PM
TimoVJL, sorry, but I can't compile your code, in PellesC .

Error in string
Code: [Select]
completedHandler->lpVtbl->AddRef = HandlerAddRef;
 error #2168: Operands of '=' have incompatible types 'unsigned long int __stdcall (*const)(ICoreWebView2CreateCoreWebView2ControllerCompletedHandler (aka struct ICoreWebView2CreateCoreWebView2ControllerCompletedHandler) *)' and 'unsigned long int __stdcall (*)(LPUNKNOWN (aka struct IUnknown *))'.

 error #2033: Assignment to const location.
Title: Re: How to convert this sh*tcode to C?
Post by: Vortex on November 25, 2023, 01:27:37 PM
Hi bitcoin,

Thank you TimoVJL!
Why don't they (micro$oft) want , that we to use the C language? First in GDI+, where you can't call them with C. Yes, a visual studio (ms sdk) does not allow use flat Api! You can try it. Only PellesC includes work!

Here are some different opinions :

https://www.quora.com/Why-does-Microsoft-use-C-as-its-main-programming-language

https://content.techgig.com/technology-guide/microsoft-azure-cto-goes-against-c-and-c-heres-why/articleshow/94473671.cms

https://analyticsindiamag.com/why-microsoft-is-dumping-c-c-for-this-new-programming-language/
Title: Re: How to convert this sh*tcode to C?
Post by: bitcoin on November 25, 2023, 01:39:26 PM
Hello, Vortex
Rust is more "better"...In future we must convert sources from Rust? No..
Title: Re: How to convert this sh*tcode to C?
Post by: TimoVJL on November 25, 2023, 01:41:05 PM
TimoVJL, sorry, but I can't compile your code, in PellesC .

Error in string
Code: [Select]
completedHandler->lpVtbl->AddRef = HandlerAddRef;
 error #2168: Operands of '=' have incompatible types 'unsigned long int __stdcall (*const)(ICoreWebView2CreateCoreWebView2ControllerCompletedHandler (aka struct ICoreWebView2CreateCoreWebView2ControllerCompletedHandler) *)' and 'unsigned long int __stdcall (*)(LPUNKNOWN (aka struct IUnknown *))'.

 error #2033: Assignment to const location.
I haven't fixed it yet, perhaps frankie can do it. MSC is more flexible with code, so those examples needs fixing.
Title: Re: How to convert this sh*tcode to C?
Post by: bitcoin on November 25, 2023, 02:09:31 PM
TimoVJL, but you can compile it in MSVC? I can't . If you can - what version of studio you use?
Title: Re: How to convert this sh*tcode to C?
Post by: Vortex on November 25, 2023, 02:10:25 PM
Hi bitcoin,

Quote
Rust is more "better"...In future we must convert sources from Rust? No..

I agree with you. Rust is just another propaganda.
Title: Re: How to convert this sh*tcode to C?
Post by: TimoVJL on November 25, 2023, 04:56:41 PM
TimoVJL, but you can compile it in MSVC? I can't . If you can - what version of studio you use?
i couldn't compile it with cl neither. So a bad example from net  >:(

WebView2 needs this too: WebView2Loader.dll

Code: [Select]
LIBRARY WebView2Loader.dll
EXPORTS
_CreateCoreWebView2EnvironmentWithOptions@16 = CreateCoreWebView2EnvironmentWithOptions

dll, include, libs :
https://www.nuget.org/packages/Microsoft.Web.WebView2

Title: Re: How to convert this sh*tcode to C?
Post by: bitcoin on November 25, 2023, 10:54:51 PM
TimoVJL, thank you! COM and new C++ is terrible!
Title: Re: How to convert this sh*tcode to C?
Post by: frankie on November 26, 2023, 01:08:28 AM
This is a quick and dirty sample, but working at least  ::).
Derived from this link  (https://stackoverflow.com/questions/66820213/basic-win32-webview2-example-not-working-in-pure-c).
Title: Re: How to convert this sh*tcode to C?
Post by: TimoVJL on November 26, 2023, 01:50:49 AM
Thanks frankie.
Don't work in Windows 7  :(

That was an important line:
Code: [Select]
webviewController->lpVtbl->AddRef(webviewController); // <-- here, increase the reference count for the webviewController
Now i can remove useless code.

WebView2 runtime :
https://developer.microsoft.com/en-us/microsoft-edge/webview2/#download-section

In Windows 7
Code: [Select]
GetProcAddress(0x75090000 [KERNEL32.DLL], "SetThreadDescription") called from "EMBEDDEDBROWSERWEBVIEW.DLL" at address 0x66EC1D51 and returned NULL. Error: The specified procedure could not be found (127).
Title: Re: How to convert this sh*tcode to C?
Post by: John Z on November 26, 2023, 11:13:45 AM
frankie, TimoVJL,

Amazed, but not surprised, you guys got this working in plain C!
I looked over the original sources posting and my eyes started watering,
what a mess IMO.

I don't know if I'll ever use it but downloaded it in case I need something
like a nightmare to keep me awake  :) or maybe just make me comatose  ;)

John Z
Title: Re: How to convert this sh*tcode to C?
Post by: bitcoin on November 26, 2023, 01:29:45 PM
Thank you  frankie!

TimoVJL, can you install microsoft edge in win7? May be this technology don't wort below win8?
Title: Re: How to convert this sh*tcode to C?
Post by: TimoVJL on November 26, 2023, 01:38:05 PM
My example was developed in Windows 7, but you need WebView2 Runtime
Distribute your app and the WebView2 Runtime (http://Distribute your app and the WebView2 Runtime)

In WebView2Test1_WS_dl.zip is for Deepl and it have minimal WebView2.h
Title: Re: How to convert this sh*tcode to C?
Post by: Vortex on November 26, 2023, 03:25:22 PM
Hi Timo and Frankie,

WebView2 runtime is installed on my Windows 7 system and I managed to run the application,many thanks.

Some academical \ philosophical questions :

What obligated M$ to invent this ugly beast of COM? Why plain API cannot to the job of COM? Are there some commecial benefits behind the creation \ invention of COM?

Quote
Everything should be made as simple as possible but not simpler.
- Albert Einstein

Quote
"Any intelligent fool can make things bigger,
more complex, and more violent.
It takes a touch of genius -- and a lot of courage --
to move in the opposite direction."
- E. F. Schumacher

Quote
A designer knows he has achieved perfection
not when there is nothing left to add,
but when there is nothing left to take away.
- Antoine de Saint Exupery

http://oby.ro/
Title: Re: How to convert this sh*tcode to C?
Post by: TimoVJL on November 27, 2023, 10:06:03 AM
It was important part of Visual Basic developments from beginning and useful for OOP languages.
Sadly it also leads to bigger programs.

https://en.wikipedia.org/wiki/Component_Object_Model
Title: Re: How to convert this sh*tcode to C?
Post by: Vortex on November 27, 2023, 09:17:42 PM
Old but an interesting thread :

Quote
The Unix model is built around the idea of lightweight processes that communicate with each other, through sockets, pipes, signals, and command lines. Historically, Unix didn't have threads (the POSIX thread model is only about 10 years old IIRC), but processes on Unix have always been much cheaper than on Windows, so it was more performant to factor functionality into separate executables than to allow a single program to grow large and monolithic.

In COM, you define binary interfaces that allow shared-memory communication. COM is tied to an object-oriented paradigm. In the classic Unix model, you define stream-oriented interfaces that allow communication over pipes, without shared memory. Conceptually, this is much closer to a functional programming paradigm.

The Unix model encourages making small programs that can be easily coupled together by a lightweight "shell", while the COM model encourages making large programs that expose "components" that can be reused by other large programs. It's really an apples-and-oranges comparison, since both models provide benefits and drawbacks for different scenarios.

Of course, modern Unix systems can have COM-like facilities. Mozilla has XPCOM, a cross-platform framework built on the same principles as COM. GNOME for a long time used Bonobo, which is conceptually very similar to Microsoft OLE, which was the forerunner to COM. But recent versions of GNOME have been shifting away from Bonobo in favor of D-Bus, which is more of an event/messaging pattern.

https://stackoverflow.com/questions/3063321/is-there-an-equivalent-to-com-on-nix-systems-if-not-what-was-the-nix-approa
Title: Re: How to convert this sh*tcode to C?
Post by: TimoVJL on November 29, 2023, 04:53:53 PM
Those, who want to develop it further  :)
Title: Re: How to convert this sh*tcode to C?
Post by: Vortex on November 29, 2023, 06:57:09 PM
Hi Timo,

Thanks, your application works fine on Windows 7 with WebView2Loader.dll
Title: Re: How to convert this sh*tcode to C?
Post by: WiiLF23 on September 30, 2024, 06:01:40 PM
Very late to this, but this is fantastic. It looks great! I couldn't' believe it. Digging further, it appears you can add link callbacks to trigger actions after a link href is clicked within the viewport. This is useful for launching windows or managing calls within the application.

However, this binds one to the Edge eco-system, which is already aggressive in marketing and overriding associations from other browsers in the registry. No Edge, no WebView2.

Very cool! Thank you.