如何使用010 Editor比较二进制文件并关联模板变量:实现差异地址与模板变量名、类型映射的技术问询
Mapping 010 Editor Compare Results to Template Variables
Great question! The 010 Editor scripting API absolutely has built-in tools to link those difference block addresses from the Compare() method back to your custom template's variable names and types. Here's a step-by-step solution:
Core Approach
- First, ensure your template is executed on both files: The API needs the template's variable metadata (address, size, name, type) to exist for each file to map addresses correctly.
- For each difference block from
Compare():- Iterate through all template variables (or use targeted lookup) to find which variables overlap with the difference address range.
- Extract the variable's name and type.
- Fetch the variable's value from both files.
- Format the output exactly as you specified:
TemplateVariableName TemplateVariableType valueFile1 valueFile2
Key 010 Editor Scripting APIs Used
RunTemplate(fileNum): Executes your loaded template on the specified file to populate variable metadata.GetVariableCount(): Returns the total number of variables created by the template.GetVariable(index): Retrieves aTVariableobject containing the variable'sname,type,address, andsize.GetVariableValue(fileNum, variable): Gets the formatted value of a variable from the specified file (handles most data types automatically).
Complete Script Example
int i, f1, f2; TVariable var; TCompareResults r; string val1, val2; // Open both target files FileOpen( "C:\\temp\\test1" ); f1 = GetFileNum(); FileOpen( "C:\\temp\\test2" ); f2 = GetFileNum(); // Run your custom template on both files (replace with your template path if it's not already loaded) RunTemplate(f1); RunTemplate(f2); // Perform the file comparison r = Compare( COMPARE_SYNCHRONIZE, f1, f2 ); // Process each detected difference block for( i = 0; i < r.count; i++ ) { // Iterate through all template variables to find matches for the current difference for(int varIdx = 0; varIdx < GetVariableCount(); varIdx++){ var = GetVariable(varIdx); // Check if the difference block overlaps with this variable's address range if( (r.record[i].startA >= var.address && r.record[i].startA < var.address + var.size) || (var.address >= r.record[i].startA && var.address < r.record[i].startA + r.record[i].sizeA) ) { // Fetch values from both files val1 = GetVariableValue(f1, var); val2 = GetVariableValue(f2, var); // Output in your required format Printf( "%s %s %s %s\n", var.name, var.type, val1, val2 ); // Optional: Break if you only want the first matching variable (remove if handling nested variables) break; } } }
Notes for Edge Cases
- Nested Variables: If your template uses structs or nested variables, you'll need a recursive function to iterate through child variables (use
GetVariableChildCount()andGetVariableChild()APIs for this). - Partial Variable Differences: The script checks for any overlap between the difference block and variable. If you need to flag partial changes (e.g., only part of a large array differs), add logic to note that the variable has partial modifications.
- Custom Value Formatting: For complex types like enums or custom structs,
GetVariableValue()might not format values exactly as you want—add custom formatting usingvar.typechecks and manual value extraction.
内容的提问来源于stack exchange,提问作者Denis Strekalov




