Internal memory has become invalid,while call dll Function

Discussion forum about all things Gupta, OpenText and the community.
SSRJMC
Saudi Arabia
Posts: 5
Joined: 08 May 2017, 12:27
Location: Jeddah,Saudi Arabia

Internal memory has become invalid,while call dll Function

Post by SSRJMC » 30 Dec 2019, 13:49

Hi Everybady,

I am getting this error (Internal memory has become invalid) at runtime while calling dll function...
function like ==> int nInitInstance(HWND hwnd, UINT nMessage )

Function: nInitInstance
Description:
Export Ordinal: 0
Returns
Number: INT
Parameters
Window Handle: HWND
Number: UINT
You do not have the required permissions to view the files attached to this post.

User avatar
markus.essmayr
Site Admin
Site Admin
Austria
Posts: 1020
Joined: 06 Mar 2017, 06:07
Location: Austria

Re: Internal memory has become invalid,while call dll Function

Post by markus.essmayr » 30 Dec 2019, 16:55

Hi,

could you please also post the code how the function is defined in the DLL?
It's really important that all parameter types match exactly and also that the correct calling convention is used (I think it was stdcall).

Max
Markus Eßmayr
teamdeveloper@t-mx.com

SSRJMC
Saudi Arabia
Posts: 5
Joined: 08 May 2017, 12:27
Location: Jeddah,Saudi Arabia

Re: Internal memory has become invalid,while call dll Function

Post by SSRJMC » 31 Dec 2019, 07:12

Hi,

I have function in a cfint32.dll which look like this : nInitInstance(HWND hwnd, UINT nMessage );
herewith i have attached screenshot how i define function, please find it
please assist me How should I set the parameters to succesfully call this function from my APP.

One of my client provide this dll to integrate into my application, I tried to call cfint32.dll functions from c#.i had successfully reached target what i want..
I used following pieces of code in C#.

using System.Runtime.InteropServices;
using Microsoft.Win32;
public class Form1 : System.Windows.Forms.Form
{

[DllImport("KERNEL32.DLL")]
public static extern int LoadLibrary(string lpLibFileName);
[DllImport("KERNEL32.DLL")]

public static extern int FreeLibrary(int hLibModule);

[DllImport("cfint32.dll")]
public static extern int nInitInstance (int WindowHandle, int EventMessage);
[DllImport("cfint32.dll")]
public static extern int nWritePacket (string InPacket, int Instance);
[DllImport("cfint32.dll")]
public static extern string sReadPacket (int Instance);
[DllImport("cfint32.dll")]
public static extern int nReleaseInstance (int Instance);

public const int CustomWindowMessage = 1025;

public int Codefinder;

public int DLLInstance;

/* Call this function */
private void Form1_Load(object sender, System.EventArgs e)
{
RegistryKey objKey;
string dllPath;

objKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\3M\\HIS\\Interface", false);
dllPath = objKey.GetValue("CfInt32DllPath").ToString();
DLLInstance = LoadLibrary(dllPath);

Codefinder = nInitInstance(this.Handle.ToInt32(), CustomWindowMessage);
}
You do not have the required permissions to view the files attached to this post.

SSRJMC
Saudi Arabia
Posts: 5
Joined: 08 May 2017, 12:27
Location: Jeddah,Saudi Arabia

Re: Internal memory has become invalid,while call dll Function

Post by SSRJMC » 31 Dec 2019, 07:49

markus.essmayr wrote:
30 Dec 2019, 16:55
Hi,

could you please also post the code how the function is defined in the DLL?
It's really important that all parameter types match exactly and also that the correct calling convention is used (I think it was stdcall).

Max
Hi,

I have function in a cfint32.dll which look like this : nInitInstance(HWND hwnd, UINT nMessage );
herewith i have attached screenshot how i define function, please find it
please assist me How should I set the parameters to succesfully call this function from my APP.

One of my client provide this dll to integrate into my application, I tried to call cfint32.dll functions from c#.i had successfully reached target what i want..
I used following pieces of code in C#.

using System.Runtime.InteropServices;
using Microsoft.Win32;
public class Form1 : System.Windows.Forms.Form
{

[DllImport("KERNEL32.DLL")]
public static extern int LoadLibrary(string lpLibFileName);
[DllImport("KERNEL32.DLL")]

public static extern int FreeLibrary(int hLibModule);

[DllImport("cfint32.dll")]
public static extern int nInitInstance (int WindowHandle, int EventMessage);
[DllImport("cfint32.dll")]
public static extern int nWritePacket (string InPacket, int Instance);
[DllImport("cfint32.dll")]
public static extern string sReadPacket (int Instance);
[DllImport("cfint32.dll")]
public static extern int nReleaseInstance (int Instance);

public const int CustomWindowMessage = 1025;

public int Codefinder;

public int DLLInstance;

/* Call this function */
private void Form1_Load(object sender, System.EventArgs e)
{
RegistryKey objKey;
string dllPath;

objKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\3M\\HIS\\Interface", false);
dllPath = objKey.GetValue("CfInt32DllPath").ToString();
DLLInstance = LoadLibrary(dllPath);

Codefinder = nInitInstance(this.Handle.ToInt32(), CustomWindowMessage);
}
You do not have the required permissions to view the files attached to this post.

User avatar
markus.essmayr
Site Admin
Site Admin
Austria
Posts: 1020
Joined: 06 Mar 2017, 06:07
Location: Austria

Re: Internal memory has become invalid,while call dll Function

Post by markus.essmayr » 31 Dec 2019, 07:56

Ah, ok. It's not a DLL developed by yourself.

Does your client provide you a .h file which you could use to import this DLL into a C/C++ application?
That could help, because a .h file contains the exact function definition as it is written to build the DLL (of course, only if the DLL is build from C/C++ source code).

Another question, you are using some source code to dynamically try to load the DLL before you call it.
I think that TD loads all external DLLs and imports all External Functions during startup of the application, so you don't need this extra code.
Just call the function wherever you want to.
(@anybody: If I'm wrong with that, please let me know!)

The code surrounding your call (which seems to be written by master Dave himself ;) ) is only necessary, of you try to verify if a DLL provides the function passed to GetProcAddressA but TD does that for you, as soon you declare it in External Functions.

Ok, but that still doesn't solve your problem.

One more question: Do you get the error message when you call the function the first time while your application runs? Or may it be, that the first call succeeds and only subsequent calls fail?

Max
Markus Eßmayr
teamdeveloper@t-mx.com

SSRJMC
Saudi Arabia
Posts: 5
Joined: 08 May 2017, 12:27
Location: Jeddah,Saudi Arabia

Re: Internal memory has become invalid,while call dll Function

Post by SSRJMC » 31 Dec 2019, 09:21

markus.essmayr wrote:
31 Dec 2019, 07:56
Ah, ok. It's not a DLL developed by yourself.

Does your client provide you a .h file which you could use to import this DLL into a C/C++ application?
That could help, because a .h file contains the exact function definition as it is written to build the DLL (of course, only if the DLL is build from C/C++ source code).

Another question, you are using some source code to dynamically try to load the DLL before you call it.
I think that TD loads all external DLLs and imports all External Functions during startup of the application, so you don't need this extra code.
Just call the function wherever you want to.
(@anybody: If I'm wrong with that, please let me know!)

The code surrounding your call (which seems to be written by master Dave himself ;) ) is only necessary, of you try to verify if a DLL provides the function passed to GetProcAddressA but TD does that for you, as soon you declare it in External Functions.

Ok, but that still doesn't solve your problem.

One more question: Do you get the error message when you call the function the first time while your application runs? Or may it be, that the first call succeeds and only subsequent calls fail?

Max
using this line : Set nProcAddress = GetProcAddress( nModuleHandle, psFunctionName )
I Pass the dll function name (psFunctionName) to verify this function available or not.
@first call ( DLL and function is found) successfully, i have a problem only in subsequent call (when ever try to invoke function ) i am getting error

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 2837
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: Internal memory has become invalid,while call dll Function

Post by Dave Rabelink » 31 Dec 2019, 11:08

markus.essmayr wrote:
31 Dec 2019, 07:56
I think that TD loads all external DLLs and imports all External Functions during startup of the application, so you don't need this extra code.
Just call the function wherever you want to.
(@anybody: If I'm wrong with that, please let me know!)
Hi Max :)

Well, at runtime a dll is loaded at the moment you call the first function from it. Before that when nothing is touching the dll, it is not loaded yet.
(when the TD runtime itself needs a specific dll, it will be loaded of course at the moment the runtime needs it).

At compile time, the TD IDE will load the dll to check the existence of the defined functions in the external functions section, when the ordinal number is set to 0 (zero).

Since TD 7.0 you can change the TD IDE behavior concerning this "check at compile time". The preferences (tab compiler) offer a new option to "Disable external functions type checking".
This is handy to be able to build/compile a project without having the dll's on the system.


So on the issue at hand here. It seems the external definition is correct, except that in TD the second parameter is set as UINT which in c# I see defined as INT.
(but that should not make a difference on message constants, but maybe worth a try to change).

What could be the case is the HWND. What does the dll do with that window handle? Does it alter properties of attributes in any way?
Or is it only used to send a msg to?

My suspicion is the LoadLibrary. That is not needed and maybe the cause of the issue.
The check function/dll functionality should be finished completely before calling the function of the dll.
So call the check function and after it returns correctly (and closes the loaded library) then call the dll function.

Beware that the loadlibrary function does not load the referenced dll's. It only loads the dll in question.
Maybe the dll needs other dll's to work properly.

So, use the check function to confirm the dll and function is present and afterwards call the dll function.

Like this:

Code: Select all

   If PALCheckDLLFunction( "cfint32.dll", "nInitInstance" ) > 0
      Call nInitInstance( ... )
   Else
      ! Show error message or perform alternative task
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

User avatar
markus.essmayr
Site Admin
Site Admin
Austria
Posts: 1020
Joined: 06 Mar 2017, 06:07
Location: Austria

Re: Internal memory has become invalid,while call dll Function

Post by markus.essmayr » 03 Jan 2020, 16:18

Hi,

thanks, Dave, for giving me this extra information about loading of DLLs at runtime.

Knowing what the DLL exactly does internally would be quite helpful to track down the issue.
As you pass a custom message ID, please ensure, that this is nothig TD uses internally.

Is it possible, that you get the DLLs source code ... at least the code of the function?

Max
Markus Eßmayr
teamdeveloper@t-mx.com

Return to “General Discussion”

Who is online

Users browsing this forum: [Ccbot] and 0 guests