BIM Coordinator Program (INT) April 22, 2024
Find the next step in your career as a Graphisoft Certified BIM Coordinator!
Archicad C++ API
About Archicad add-on development using the C++ API.

Seeking Help: Unexpected Archicad API Crash After Running an External EXE File

4livesdragon
Contributor

Hello everyone, I am a novice who recently started learning to develop the Archicad API. Thank you all for the help provided before, and now I've encountered a very perplexing problem.

 

I ran an exe file with C language. This file was written in Python and uses utf-8 encoding (I'm not sure if it's relevant). Here's the relevant code:


setlocale(LC_ALL, "");
int ret = system("demo.exe");
if(ret == -1)
{WriteReport_Alert("Not Running!");}
else
{WriteReport_Alert("Done!");}
break;

 

This exe file, in theory, has absolutely nothing to do with Archicad. It just reads some files and outputs a file.

 

After running the above code in Archicad separately, a strange bug appeared. I am not sure if I successfully ran this exe file, but all the functions I wrote previously related to creating Objects stopped working. When I try to call them, Archicad crashes directly. These functions are all very basic, just modifying some parameters such as position and size information, and calling the ACAPI_Element_Create and ACAPI_Element_Change functions. Moreover, my exe file, when not opened through Archicad, gives the error "Unhandled exception in script" when double-clicked.

 

As my information is in Chinese, I used a lot of wide characters wchar_t in the code. I'm not sure if this is relevant, but this is the direction I currently suspect. Because not all the functions I wrote are malfunctioning. The functions that simply process information through C language are usable, and outputting Object information (including Chinese) from Archicad is also usable. It's only when I call the function to create Objects and ACAPI_Element_Change that it crashes.

 

That's the situation I'm facing. I was wondering if anyone has encountered the same problem and hope everyone could provide me with some solutions or directions to explore!

 

Thank you very much!

7 REPLIES 7
Joel Buehler
Enthusiast

im not to sure if i get you right, but do you have two problems or one? 

 

so does archicad crashes now without running the external exe or does it crash now with running the external exe?

 

im not sure from waht your problems exactly come, but i can give a few tipps:

 

the api has problems with special character casting.

if you do something like this: 

GS::UniString = "This is a UniString with a äää" you end up with this:

 

JoelBuehler_0-1688817628750.png

Strings loaded from a resource file with special characters like a ä ö ü do not work! 

so maybe your problem has something to do with your special characters. 

 

furthermore you get a lot of headache if you use wchar_t since all api calls use the GS::UniString. it is the best approach to use wchar_t only if really needed, but you should convert to the UniString when possible. 

 

furhtermore is the api very crash resistand if you work with the error codes. if you dont use them and dont check for those you get unforseen behavior. 

archicad crashed without a fight if you do a null reference. no questions asked, its out. i expect your problem to be in this area, meaning your problem could be that you do a null reference.   

 

 

 

 


@Joel Buehler wrote:

 

Strings loaded from a resource file with special characters like a ä ö ü do not work! 

so maybe your problem has something to do with your special characters. 

This is only true if the resource file is encoded incorrectly.
You can solve this problem by encoding your resource files in UTF-8!
Then you can load strings into GS::UniString with e.g. RSGetIndString.
Also dialogs handle it correctly then.

Bernd Schwarzenbacher - Archicad Add-On Developer - Get Add-Ons & Archicad Tips on my Website: Archi-XT.com

oh i see, i used ACAPI_Resource_GetFixStr instead of RSGetIndString. 

buth where do i tell archicad that my *.grc file is uft-8? 

 

@BerndSchwarzenbacher  Thank you very much! 😃 

UTF-8 encoding is a property about the text-file itself!

You don't have to tell Archicad or the compiler about it.

They should recognize it automatically in most cases.

 

Check File Encoding

In your command line you can check the encoding with the command `file` on (Windows & macOS).
Here are two examples with different encodings:

 

> file .\RAUT\AddOn.grc
.\RAUT\AddOn.grc: C source, Unicode text, UTF-8 (with BOM) text
> file .\RINT\AddOn.grc
.\RINT\AddOn.grc: C source, ASCII text

 

 

Conversion

A lot of text editors offer options to save text files with specific encodings.

 

Otherwise you can also convert files in your CLI:

  • Windows PowerShell:
     > Get-Content .\infile-with-some-encoding.txt | Set-Content -Encoding utf-8 outfile-now-in-utf8.md​
  • macOSL: you should have iconv to do the conversion.
Bernd Schwarzenbacher - Archicad Add-On Developer - Get Add-Ons & Archicad Tips on my Website: Archi-XT.com

The most puzzling thing about this issue is that after I run this exe program once with Archicad, it crashes even without running the exe. After my tests, this error results in ACAPI_CallUndoableCommand not being able to return 0 (NoError). Whenever this function returns 0, the system crashes. However, if it does not return 0, it means that something has already gone wrong. The same code still works stably on computers that have not run this exe file. I'm not sure if running this exe has changed any settings in Archicad, which I find very baffling. But after I reinstalled Archicad and reconfigured the environment, other functions that include ACAPI_CallUndoableCommand became usable again.

 

I've currently found a preliminary solution to this problem, but I'm not sure if this is the fundamental cause of the issue. It might be due to the fact that the encoding format of the text exported by one of my functions is not fixed (when it contains Chinese, the encoding format is ANSI, but when it only has numbers and English, the encoding format is UTF-8), and this text file is then used by the previously mentioned exe file. After I fixed the encoding format to always be ANSI, the exe file could be run in Archicad, and running it did not lead to other functions becoming unusable. This is the current situation, and I will continue testing subsequently.

wild guess, but such unforseen behavior can also be explained by memmory violations. is there a chance that you use pointers? furthermore, when the app crashes without running the exe, has your Add-On be loaded into the memmory of archicad?  since an add-on can only work in the archicad adress space of the memmory, would explain wy archicad gets broken and nothing else. 

4livesdragon
Contributor

Yeah, I agree with your point of view. I believe there are some memory violations in my code. When Archicad crashes without running the exe file, I commented out all the code related to the exe, but it still crashed. This might indicate some sort of conflict happening somewhere.

Learn and get certified!